Even numbers at even index and odd numbers at odd index
Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index. Required auxiliary space is O(1).
Examples :
Input : arr[] = {3, 6, 12, 1, 5, 8} Output : 6 3 12 1 8 5 Input : arr[] = {10, 9, 7, 18, 13, 19, 4, 20, 21, 14} Output : 10 9 18 7 20 19 4 13 14 21
Source: Amazon Interview Experience | Set 410.
Approach :
- Start from the left and keep two index one for even position and other for odd positions.
- Traverse these index from left.
- At even position there should be even number and at odd positions, there should be odd number.
- Whenever there is mismatch , we swap the values at odd and even index.
Below is the implementation of the above approach :
CPP
// C++ implementation to arrange // odd and even numbers #include <bits/stdc++.h> using namespace std; // function to arrange odd and even numbers void arrangeOddAndEven( int arr[], int n) { int oddInd = 1; int evenInd = 0; while ( true ) { while (evenInd < n && arr[evenInd] % 2 == 0) evenInd += 2; while (oddInd < n && arr[oddInd] % 2 == 1) oddInd += 2; if (evenInd < n && oddInd < n) swap (arr[evenInd], arr[oddInd]); else break ; } } // function to print the array void printArray( int arr[], int n) { for ( int i = 0; i < n; i++) cout << arr[i] << " " ; } // Driver program to test above int main() { int arr[] = { 3, 6, 12, 1, 5, 8 }; int n = sizeof (arr) / sizeof (arr[0]); cout << "Original Array: " ; printArray(arr, n); arrangeOddAndEven(arr, n); cout << "\nModified Array: " ; printArray(arr, n); return 0; } |
Java
// Java implementation to // arrange odd and even numbers import java.util.*; import java.lang.*; class GfG { // function to arrange // odd and even numbers public static void arrangeOddAndEven( int arr[], int n) { int oddInd = 1 ; int evenInd = 0 ; while ( true ) { while (evenInd < n && arr[evenInd] % 2 == 0 ) evenInd += 2 ; while (oddInd < n && arr[oddInd] % 2 == 1 ) oddInd += 2 ; if (evenInd < n && oddInd < n) { int temp = arr[evenInd]; arr[evenInd] = arr[oddInd]; arr[oddInd] = temp; } else break ; } } // function to print the array public static void printArray( int arr[], int n) { for ( int i = 0 ; i < n; i++) System.out.print(arr[i] + " " ); } // Driver function public static void main(String argc[]){ int arr[] = { 3 , 6 , 12 , 1 , 5 , 8 }; int n = 6 ; System.out.print( "Original Array: " ); printArray(arr, n); arrangeOddAndEven(arr, n); System.out.print( "\nModified Array: " ); printArray(arr, n); } } // This code is contributed by Sagar Shukla |
Python3
# Python3 implementation to # arrange odd and even numbers def arrangeOddAndEven(arr, n): oddInd = 1 evenInd = 0 while ( True ): while (evenInd < n and arr[evenInd] % 2 = = 0 ): evenInd + = 2 while (oddInd < n and arr[oddInd] % 2 = = 1 ): oddInd + = 2 if (evenInd < n and oddInd < n): temp = arr[evenInd] arr[evenInd] = arr[oddInd] arr[oddInd] = temp; else : break # function to print the array def printArray(arr, n): for i in range ( 0 ,n): print (arr[i] , " ",end=" ") # Driver function def main(): arr = [ 3 , 6 , 12 , 1 , 5 , 8 ] n = 6 print ( "Original Array: " ,end = "") printArray(arr, n) arrangeOddAndEven(arr, n) print ( "\nModified Array: " ,end = "") printArray(arr, n) if __name__ = = '__main__' : main() # This code is contributed by 29AjayKumar |
C#
// C# implementation to // arrange odd and even numbers using System; class GFG { // function to arrange // odd and even numbers public static void arrangeOddAndEven( int [] arr, int n) { int oddInd = 1; int evenInd = 0; while ( true ) { while (evenInd < n && arr[evenInd] % 2 == 0) evenInd += 2; while (oddInd < n && arr[oddInd] % 2 == 1) oddInd += 2; if (evenInd < n && oddInd < n) { int temp = arr[evenInd]; arr[evenInd] = arr[oddInd]; arr[oddInd] = temp; } else break ; } } // function to print the array public static void printArray( int [] arr, int n) { for ( int i = 0; i < n; i++) Console.Write(arr[i] + " " ); } // Driver function public static void Main() { int [] arr = { 3, 6, 12, 1, 5, 8 }; int n = 6; Console.Write( "Original Array: " ); printArray(arr, n); arrangeOddAndEven(arr, n); Console.Write( "\nModified Array: " ); printArray(arr, n); } } // This code is contributed by Sam007 |
Javascript
<script> // Javascript implementation to arrange // odd and even numbers // function to arrange odd and even numbers function arrangeOddAndEven(arr, n) { let oddInd = 1; let evenInd = 0; while ( true ) { while (evenInd < n && arr[evenInd] % 2 == 0) evenInd += 2; while (oddInd < n && arr[oddInd] % 2 == 1) oddInd += 2; if (evenInd < n && oddInd < n) { let temp; temp = arr[evenInd]; arr[evenInd] = arr[oddInd]; arr[oddInd] = temp; } else break ; } } // function to print the array function printArray(arr, n) { for (let i = 0; i < n; i++) document.write(arr[i] + " " ); } // Driver program to test above let arr = [ 3, 6, 12, 1, 5, 8 ]; let n = arr.length; document.write( "Original Array: " ); printArray(arr, n); arrangeOddAndEven(arr, n); document.write( "<br>" + "Modified Array: " ); printArray(arr, n); // This code is contributed by Mayank Tyagi </script> |
Original Array: 3 6 12 1 5 8 Modified Array: 6 3 12 1 8 5
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Approach: Two-Pointer Swap
The steps to solve the problem using this approach are as follows:
- Initialize two pointers i and j to 0 and 1, respectively. These pointers will help us to keep track of the current indices of the elements in the array that we are processing.
- Loop through the array until i or j reaches the end of the array.
- If the element at index i is odd and the element at index j is even, swap them. This will ensure that the even number is moved to an even index and odd number is moved to an odd index.
- Increment both i and j by 2 to move them to the next odd and even index respectively.
- If the element at index i is already even, increment i by 2.
- Similarly, if the element at index j is already odd, increment j by 2.
- Once we have looped through the entire array, the even numbers will be at even indices and odd numbers will be at odd indices.
- Return the modified array.
C++
#include <iostream> #include <vector> using namespace std; vector< int > arrange_array(vector< int > arr) { int i = 0; int j = 1; int n = arr.size(); while (i < n && j < n) { if (arr[i] % 2 != 0 && arr[j] % 2 == 0) { swap(arr[i], arr[j]); i += 2; j += 2; } else { if (arr[i] % 2 == 0) { i += 2; } if (arr[j] % 2 != 0) { j += 2; } } } return arr; } int main() { // Example 1 vector< int > arr1 = {3, 6, 12, 1, 5, 8}; vector< int > arranged_arr1 = arrange_array(arr1); for ( auto i : arranged_arr1) { cout << i << " " ; } cout << endl; // Example 2 vector< int > arr2 = {10, 9, 7, 18, 13, 19, 4, 20, 21, 14}; vector< int > arranged_arr2 = arrange_array(arr2); for ( auto i : arranged_arr2) { cout << i << " " ; } cout << endl; return 0; } |
Python3
def arrange_array(arr): i = 0 j = 1 n = len (arr) while i < n and j < n: if arr[i] % 2 ! = 0 and arr[j] % 2 = = 0 : arr[i], arr[j] = arr[j], arr[i] i + = 2 j + = 2 else : if arr[i] % 2 = = 0 : i + = 2 if arr[j] % 2 ! = 0 : j + = 2 return arr # Example 1 arr = [ 3 , 6 , 12 , 1 , 5 , 8 ] arranged_arr = arrange_array(arr) print (arranged_arr) # Output: [6, 3, 12, 1, 8, 5] #Example arr = [ 10 , 9 , 7 , 18 , 13 , 19 , 4 , 20 , 21 , 14 ] arranged_arr = arrange_array(arr) print (arranged_arr) # Output: [10, 9, 18, 7, 20, 19, 4, 13, 14, 21] |
[6, 3, 12, 1, 8, 5] [10, 9, 18, 7, 20, 19, 4, 13, 14, 21]
This algorithm runs in O(n) time complexity and O(1) space complexity
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...