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 1Input: 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 <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 |
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 |
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.
Recommended Posts:
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Rearrange array such that even positioned are greater than odd
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Rearrange array by interchanging positions of even and odd elements in the given array
- Rearrange array such that difference of adjacent elements is in descending order
- Rearrange an array such that product of every two consecutive elements is a multiple of 4
- Rearrange array such that sum of same indexed elements is atmost K
- Rearrange a given list such that it consists of alternating minimum maximum elements
- Rearrange two given arrays such that sum of same indexed elements lies within given range
- Rearrange all elements of array which are multiples of x in increasing order
- Rearrange all elements of array which are multiples of x in decreasing order
- Permutation of Array such that products of all adjacent elements are even
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Minimum elements to be removed such that sum of adjacent elements is always even
- Python | Number of elements to be removed such that product of adjacent elements is always even
- Rearrange an array such that 'arr[j]' becomes 'i' if 'arr[i]' is 'j' | Set 1
- Rearrange an array such that every odd indexed element is greater than it previous
- Rearrange numbers in an array such that no two adjacent numbers are same
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.

