 Open in App
Not now

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

• Difficulty Level : Medium
• Last Updated : 31 May, 2022

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++

 `// C++ Program to implement``// the above approach``#include ``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);``    ``checkPossible(arr, n);``    ``return` `0;``}` `// This code is contributed by gauravrajput1`

## Java

 `// 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);``    ``}``}`

## Python3

 `# 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`

## C#

 `// 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`

## Javascript

 ``

Output:

```Yes
3 2 5 4 7 6```

Time Complexity: O(N), as we are using a loop to traverse N times.

Auxiliary Space: O(1), as we are not using any extra space.

My Personal Notes arrow_drop_up