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.
Examples:
Input : 1 9 5 3 2 6 7 11
Output : 6 2 3 5 7 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.
Approach :
- Start two pointers from left and right of the array.
- Create a new array of same size as given.
- If the element at left or right is even then put it in front of the array else at the end.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void arrayEvenAndOdd( int arr[], int n) {
int b[n];
int k = 0, l = n - 1, i, j;
for (i = 0, j = n - 1; i < j; i++, j--) {
if (arr[i] % 2 == 0) {
b[k] = arr[i];
k++;
} else {
b[l] = arr[i];
l--;
}
if (arr[j] % 2 == 0) {
b[k] = arr[j];
k++;
} else {
b[l] = arr[j];
l--;
}
}
b[i] = arr[i];
for ( int i = 0; i < n; i++)
cout << b[i] << " " ;
}
int main() {
int arr[] = {1, 3, 2, 4, 7, 6, 9, 10};
int n = sizeof (arr) / sizeof ( int );
arrayEvenAndOdd(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class arr
{
static void arrayEvenAndOdd( int arr[], int n)
{
int b[] = new int [n];
int k = 0 , l = n - 1 , i, j;
for (i = 0 , j = n - 1 ; i < j; i++, j--)
{
if (arr[i] % 2 == 0 )
{
b[k] = arr[i];
k++;
}
else
{
b[l] = arr[i];
l--;
}
if (arr[j] % 2 == 0 )
{
b[k] = arr[j];
k++;
}
else
{
b[l] = arr[j];
l--;
}
}
b[i] = arr[i];
for (i = 0 ; i < n; i++)
{
System.out.print(b[i] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 };
int n = arr.length;
arrayEvenAndOdd(arr, n);
}
}
|
Python3
def arrayEvenAndOdd(arr,n):
b = [ 0 ] * n
k = 0
l = n - 1
i = 0
j = n - 1
while (i < j):
if (arr[i] % 2 = = 0 ):
b[k] = arr[i]
k + = 1
else :
b[l] = arr[i]
l - = 1
if (arr[j] % 2 = = 0 ):
b[k] = arr[j]
k + = 1
else :
b[l] = arr[j]
l - = 1
i + = 1
j - = 1
b[i] = arr[i]
for i in range ( 0 , n):
print (b[i],end = " " )
arr = [ 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 ]
n = len (arr)
arrayEvenAndOdd(arr, n)
|
C#
using System;
class GFG {
static void arrayEvenAndOdd( int []arr, int n)
{
int []b = new int [n];
int k = 0, l = n - 1, i, j;
for (i = 0, j = n - 1; i < j; i++, j--)
{
if (arr[i] % 2 == 0)
{
b[k] = arr[i];
k++;
}
else
{
b[l] = arr[i];
l--;
}
if (arr[j] % 2 == 0)
{
b[k] = arr[j];
k++;
}
else
{
b[l] = arr[j];
l--;
}
}
b[i] = arr[i];
for (i = 0; i < n; i++)
{
Console.Write(b[i] + " " );
}
}
public static void Main()
{
int []arr = {1, 3, 2, 4, 7, 6, 9, 10};
int n = arr.Length;
arrayEvenAndOdd(arr, n);
}
}
|
Javascript
<script>
function arrayEvenAndOdd(arr,n)
{
let b=[n];
let k = 0, l = n - 1, i, j;
for (i = 0, j = n - 1; i < j; i++, j--)
{
if (arr[i] % 2 == 0)
{
b[k] = arr[i];
k++;
}
else
{
b[l] = arr[i];
l--;
}
if (arr[j] % 2 == 0)
{
b[k] = arr[j];
k++;
}
else
{
b[l] = arr[j];
l--;
}
}
b[i] = arr[i];
for (i = 0; i < n; i++)
{
document.write(b[i] + " " );
}
}
let arr = [1, 3, 2, 4, 7, 6, 9, 10];
let n = arr.length;
arrayEvenAndOdd(arr, n);
</script>
|
Time Complexity : O(n/2)
Auxiliary Space : O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
03 Aug, 2022
Like Article
Save Article