Skip to content
Related Articles

Related Articles

Segregate even and odd numbers | Set 2

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 03 Aug, 2022
View Discussion
Improve Article
Save Article

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 : 

  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.

Implementation:

C++




// CPP code to segregate even odd
// numbers in an array
#include <bits/stdc++.h>
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] * # 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.

Javascript




<script>
// Java  scriptcode to segregate even odd
// numbers in an array
 
 
    // Function to segregate even odd numbers
    function arrayEvenAndOdd(arr,n)
    {
        // To store result
        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--;
            }
        }
     
        // for i == j in case of odd length
        b[i] = arr[i];
         
        // Printing segregated array
        for (i = 0; i < n; i++)
        {
            document.write(b[i] + " ");
        }
    }
     
    // Driver code
     
        let arr = [1, 3, 2, 4, 7, 6, 9, 10];
        let n = arr.length;
        arrayEvenAndOdd(arr, n);
 
 
// This code is contributed by sravan kumar Gottumukkala
</script>

Output: 

10 2 6 4 7 9 3 1

 

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!