Separate even and odd integers in a given array

Objec­tive:  Given an array which contains even and odd integers. Write an algorithm to separate even and odd numbers.


int [] arrA = {1,2,3,4,6,8,7,12};
Output: [12, 2, 8, 4, 6, 3, 7, 1]

Approach: Swapping Indexes

  • Use two indexes, left and right.
  • Put left index at the start of array and right at the end of the array.
  • Increment left till odd number is not found.
  • Decrement right till even number is not found.
  • Swap left and right elements
  • Do it till left<right

Time Complexity: O(n)



Rearranging arrays using left and right indexes
[12, 2, 8, 4, 6, 3, 7, 1]

Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

%d bloggers like this: