# Segregate even and odd numbers | Set 2

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 2 9 11 1

Input : 1 3 2 4 7 6 9 10
Output : 10 2 6 4 7 9 3 1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 :
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.

## C++

 `// CPP code to segregate even odd ` `// numbers in an array ` `#include ` `using` `namespace` `std; ` ` `  `// Function to segregate even odd numbers ` `void` `arrayEvenAndOdd(``int` `arr[], ``int` `n) { ` ` `  `  ``int` `b[n];  ``// To store result ` `  ``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--; ` `    ``} ` `  ``} ` ` `  `  ``// for i == j in case of odd length ` `  ``b[i] = arr[i]; ` ` `  `  ``// Printing segregated array ` `  ``for` `(``int` `i = 0; i < n; i++)  ` `     ``cout << b[i] << ``" "``;   ` `} ` ` `  `// Driver code ` `int` `main() { ` `  ``int` `arr[] = {1, 3, 2, 4, 7, 6, 9, 10}; ` `  ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``); ` `  ``arrayEvenAndOdd(arr, n); ` `  ``return` `0; ` `} `

## Java

 `// Java code to segregate even odd ` `// numbers in an array ` `import` `java.util.Arrays; ` ` `  `class` `arr ` `{ ` `    ``// Function to segregate even odd numbers ` `    ``static` `void` `arrayEvenAndOdd(``int` `arr[], ``int` `n)  ` `    ``{ ` `        ``// To store result ` `        ``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--; ` `            ``} ` `        ``} ` `     `  `        ``// for i == j in case of odd length ` `        ``b[i] = arr[i]; ` `         `  `        ``// Printing segregated array ` `        ``for` `(i = ``0``; i < n; i++)  ` `        ``{ ` `            ``System.out.print(b[i] + ``" "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `arr[] = {``1``, ``3``, ``2``, ``4``, ``7``, ``6``, ``9``, ``10``}; ` `        ``int` `n = arr.length; ` `        ``arrayEvenAndOdd(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed  ` `// by Smitha Dinesh Semwal `

## Python3

 `# Python3 code to segregate even odd ` `# numbers in an array ` ` `  `# Function to segregate even odd numbers ` `def` `arrayEvenAndOdd(arr,n):   ` ` `  `    ``b ``=` `[``0``] ``*` `n  ``# To store result ` `    ``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` `      `  `  `  ` `  `    ``# for i == j in case of odd length ` `    ``b[i] ``=` `arr[i]  ` ` `  `    ``# Printing segregated array ` `    ``for` `i ``in` `range``(``0``, n):  ` `        ``print``(b[i],end``=``" "``)   ` `  `  ` `  `# Driver code ` `arr ``=`  `[``1``, ``3``, ``2``, ``4``, ``7``, ``6``, ``9``, ``10``]   ` `n ``=` `len``(arr) ` ` `  `arrayEvenAndOdd(arr, n) ` ` `  `# This code is contributed by ` `# Smitha Dinesh Semwal `

## C#

 `// C# code to segregate even odd ` `// numbers in an array ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to segregate even odd numbers ` `    ``static` `void` `arrayEvenAndOdd(``int` `[]arr, ``int` `n)  ` `    ``{ ` `         `  `        ``// To store result ` `        ``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--; ` `            ``} ` `        ``} ` `     `  `        ``// for i == j in case of odd length ` `        ``b[i] = arr[i]; ` `         `  `        ``// Printing segregated array ` `        ``for` `(i = 0; i < n; i++)  ` `        ``{ ` `            ``Console.Write(b[i] + ``" "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `[]arr = {1, 3, 2, 4, 7, 6, 9, 10}; ` `        ``int` `n = arr.Length; ` `        ``arrayEvenAndOdd(arr, n); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

Output:

```10 2 6 4 7 9 3 1
```

Time Complexity : O(n/2)
Auxiliary Space : O(n)

My Personal Notes arrow_drop_up

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 contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.