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++
#include <bits/stdc++.h>
using namespace std;
void checkPossible( int a[], int n)
{
int even_no_count = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] % 2 == 0)
even_no_count++;
}
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] << " " ;
}
}
int main()
{
int arr[] = { 2, 3, 4, 5, 6, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
checkPossible(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void checkPossible( int a[])
{
int even_no_count = 0 ;
for ( int i = 0 ; i < a.length; i++) {
if (a[i] % 2 == 0 )
even_no_count++;
}
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] + " " );
}
}
public static void main(String args[])
{
int arr[] = { 2 , 3 , 4 , 5 , 6 , 7 };
checkPossible(arr);
}
}
|
Python3
def checkPossible(a, n):
even_no_count = 0
for i in range (n):
if (a[i] % 2 = = 0 ):
even_no_count + = 1
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 = " " )
arr = [ 2 , 3 , 4 , 5 , 6 , 7 ]
n = len (arr)
checkPossible(arr, n)
|
C#
using System;
class GFG{
static void checkPossible( int []a)
{
int even_no_count = 0;
for ( int i = 0; i < a.Length; i++)
{
if (a[i] % 2 == 0)
even_no_count++;
}
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] + " " );
}
}
public static void Main(String []args)
{
int []arr = { 2, 3, 4, 5, 6, 7 };
checkPossible(arr);
}
}
|
Javascript
<script>
function checkPossible(a)
{
let even_no_count = 0;
for (let i = 0; i < a.length; i++) {
if (a[i] % 2 == 0)
even_no_count++;
}
if (a.length / 2 > even_no_count) {
document.write( "No<br>" );
return ;
}
document.write( "Yes<br>" );
let j = 0;
for (let 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 (let i = 0; i < a.length; i++) {
document.write(a[i] + " " );
}
}
let arr = [ 2, 3, 4, 5, 6, 7 ];
checkPossible(arr);
</script>
|
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.
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...