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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Segregate even and odd numbers | Set 2
- Segregate Even and Odd numbers
- Even numbers at even index and odd numbers at odd index
- Segregate 0s and 1s in an array
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Check whether bitwise AND of N numbers is Even or Odd
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Array Index with same count of even or odd numbers on both sides
- Check whether bitwise OR of N numbers is Even or Odd
- Represent N as sum of K even or K odd numbers with repetitions allowed
- Sum of even numbers at even position
- Number of Subsequences with Even and Odd Sum | Set 2
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Sort even-placed elements in increasing and odd-placed in decreasing order
- Program to print Sum of even and odd elements 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.