Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Separate even and odd integers in a given array

Objec­tive:  Given an array which con­tains even and odd inte­gers. Write an algo­rithm to sep­a­rate even and odd numbers.

Exam­ple

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

Approach: Swap­ping Indexes

  • Use two indexes, left and right.
  • Put left index at the start of array and right at the end of the array.
  • Incre­ment left till odd num­ber is not found.
  • Decre­ment right till even num­ber is not found.
  • Swap left and right elements
  • Do it till left<right

Time Com­plex­ity: O(n)

Code:

Out­put:

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

You may also like...