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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Segregate even and odd numbers | Set 2
- Segregate Even and Odd numbers
- Segregate 0s and 1s in an array
- Find ratio of zeroes, positive numbers and negative numbers in the Array
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- 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
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Given pairwise sum of n numbers, find the numbers
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- 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
- Sum of even numbers at even position
- Sort 3 numbers
- Sum of two large numbers
- Even numbers at even index and odd numbers at odd index
- Almost Prime Numbers
- Economical Numbers
- GCD of more than two (or array) 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.