Rearrange array such that all even-indexed elements in the Array is even

Given an array arr[], the task is to check if it is possible to rearrange the array in such a way that every even index(1-based indexing) contains an even number. If such a rearrangement is not possible, print “No”. Otherwise, print “Yes” and print a possible arrangement

Examples:

Input: arr[] = {2, 4, 8, 3, 1} 
Output: 
Yes 
3 4 8 2 1

Input: arr[] = {3, 3, 11, 8} 
Output: No 
Explanation: Since, the array contains only one even element, all even indices cannot be filled with a even elements.

Approach: 
Follow the steps below to solve the problem: 



  • Count the total number of even elements present in the given array. If the count exceeds the total number of even indices in the given array, print “No”.
  • Otherwise, print “Yes”. Now, traverse the array using two pointers, i and j, pointing to even and odd indices respectively.
  • For any ith index containing an odd element, iterate odd indices using j until an even element is encountered.
  • Swap a[i] and a[j]
  • Repeat the above steps until all even indices are filled with an even element.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if it the
// array can be rearranged such
// such that every even indices
// contains an even element
void checkPossible(int a[], int n)
{
    // Stores the count of even elements
    int even_no_count = 0;
  
    // Traverse array to count even numbers
    for (int i = 0; i < n; i++) 
    {
        if (a[i] % 2 == 0)
            even_no_count++;
    }
  
    // If even_no_count exceeds
    // count of even indices
    if (n / 2 > even_no_count) 
    {
        cout << "No" << endl;
        return;
    }
  
    cout << "Yes" << endl;
    int j = 0;
    for (int i = 1; i < n; i += 2) 
    {
        if (a[i] % 2 == 0)
            continue;
        else 
        {
            while (j < n && a[j] % 2 != 0)
                j += 2;
  
            a[i] += a[j];
            a[j] = a[i] - a[j];
            a[i] -= a[j];
        }
    }
  
    for (int i = 0; i < n; i++) 
    {
        cout << a[i] << " ";
    }
}
  
// Driver Code
int main()
{
  
    int arr[] = { 2, 3, 4, 5, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    checkPossible(arr, n);
    return 0;
}
  
// This code is contributed by gauravrajput1

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to implement
// the above approach
import java.io.*;
import java.util.*;
  
class GFG {
  
    // Function to check if it the
    // array can be rearranged such
    // such that every even indices
    // contains an even element
    static void checkPossible(int a[])
    {
        // Stores the count of even elements
        int even_no_count = 0;
  
        // Traverse array to count even numbers
        for (int i = 0; i < a.length; i++) {
  
            if (a[i] % 2 == 0)
                even_no_count++;
        }
  
        // If even_no_count exceeds
        // count of even indices
        if (a.length / 2 > even_no_count) {
            System.out.println("No");
            return;
        }
  
        System.out.println("Yes");
        int j = 0;
        for (int i = 1; i < a.length; i += 2) {
            if (a[i] % 2 == 0)
                continue;
            else {
                while (j < a.length && a[j] % 2 != 0)
                    j += 2;
  
                a[i] += a[j];
                a[j] = a[i] - a[j];
                a[i] -= a[j];
            }
        }
  
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        int arr[] = { 2, 3, 4, 5, 6, 7 };
  
        checkPossible(arr);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
  
# Function to check if it the
# array can be rearranged such
# such that every even indices
# contains an even element
def checkPossible(a, n):
      
    # Stores the count of even elements
    even_no_count = 0
  
    # Traverse array to count even numbers
    for i in range(n):
        if (a[i] % 2 == 0):
            even_no_count += 1
      
    # If even_no_count exceeds
    # count of even indices
    if (n // 2 > even_no_count):
        print("No")
        return
      
    print("Yes"
    j = 0
      
    for i in range(1, n, 2):
        if (a[i] % 2 == 0):
            continue
              
        else:
            while (j < n and a[j] % 2 != 0):
                j += 2
  
            a[i] += a[j]
            a[j] = a[i] - a[j]
            a[i] -= a[j]
          
    for i in range(n): 
        print(a[i], end = " ")
      
# Driver Code
arr = [ 2, 3, 4, 5, 6, 7 ]
n = len(arr)
  
checkPossible(arr, n)
  
# This code is contributed by code_hunt

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to implement
// the above approach
using System;
class GFG{
  
  // Function to check if it the
  // array can be rearranged such
  // such that every even indices
  // contains an even element
  static void checkPossible(int []a)
  {
    // Stores the count of even elements
    int even_no_count = 0;
  
    // Traverse array to count even numbers
    for (int i = 0; i < a.Length; i++) 
    {
      if (a[i] % 2 == 0)
        even_no_count++;
    }
  
    // If even_no_count exceeds
    // count of even indices
    if (a.Length / 2 > even_no_count) 
    {
      Console.WriteLine("No");
      return;
    }
  
    Console.WriteLine("Yes");
    int j = 0;
    for (int i = 1; i < a.Length; i += 2)
    {
      if (a[i] % 2 == 0)
        continue;
      else 
      {
        while (j < a.Length && a[j] % 2 != 0)
          j += 2;
  
        a[i] += a[j];
        a[j] = a[i] - a[j];
        a[i] -= a[j];
      }
    }
  
    for (int i = 0; i < a.Length; i++)
    {
      Console.Write(a[i] + " ");
    }
  }
  
  // Driver Code
  public static void Main(String []args)
  {
    int []arr = { 2, 3, 4, 5, 6, 7 };
  
    checkPossible(arr);
  }
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output: 

Yes
3 2 5 4 7 6

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.