Be the first user to complete this post

  • 0
Add to List
Beginner

241. Separate even and odd integers in a given array

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

Example

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 the left index at the start of the array and right at the end of the array.
  • Increment left till odd number is not found.
  • Decrement right till an even number is not found.
  • Swap left and right elements
  • Do it till left<right

Time Complexity: O(n)

Output:

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