Given an array of integers, segregate even and odd numbers in the array. All the even numbers should be present first, and then the odd numbers.
Input : 1 9 5 3 2 6 7 11 Output : 2 6 5 3 1 9 7 11 Input : 1 3 2 4 7 6 9 10 Output : 2 4 6 10 7 1 9 3
We have discussed two different approaches in below posts:
The idea discussed in this post is based on Lomuto’s Partition Scheme
- Maintain a pointer to the position before first odd element in the array.
- Traverse the array and if even number is encountered then swap it with the first odd element.
- Continue the traversal.
2 4 6 10 7 1 9 3
Time Complexity : O(n)
Auxiliary Space : O(1)
- Segregate Even and Odd numbers
- Segregate even and odd numbers | Set 2
- Segregate 0s and 1s in an array
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Rearrange numbers in an array such that no two adjacent numbers are same
- Given pairwise sum of n numbers, find the numbers
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Even numbers at even index and odd numbers at odd index
- Sort 3 numbers
- Sum of even numbers at even position
- Almost Prime Numbers
- Ugly Numbers
- Sum of two large numbers
- GCD of more than two (or array) numbers
- XOR of all Prime numbers in an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.