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 : 6 2 3 5 2 9 11 1 Input : 1 3 2 4 7 6 9 10 Output : 10 2 6 4 7 9 3 1
We have discussed one approach in Segregate Even and Odd numbers. In this post, a different simpler approach is discussed that uses an extra array.
1. Start two pointers from left and right of the array.
2. Create a new array of same size as given.
3. If the element at left or right is even then put it in front of the array else at the end.
10 2 6 4 7 9 3 1
Time Complexity : O(n/2)
Auxiliary Space : O(n)
- Segregate even and odd numbers | Set 3
- 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
- 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
- Sum of even numbers at even position
- Sum of two large numbers
- GCD of more than two (or array) numbers
- Almost Prime Numbers
- Ugly Numbers
- Sum of all odd factors of numbers in the range [l, r]
- Minimum sum of multiplications of n numbers
This article is contributed by Navin Bisht. 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.