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 | Set 2
- Segregate Even and Odd numbers
- 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
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Rearrange numbers in an array such that no two adjacent numbers are same
- Given pairwise sum of n numbers, find the numbers
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Sort 3 numbers
- Even numbers at even index and odd numbers at odd index
- Sum of even numbers at even position
- GCD of more than two (or array) numbers
- Sum of two large numbers
- Ugly Numbers
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.