Minimize swaps to rearrange array such that parity of index and corresponding element is same
Last Updated :
10 Mar, 2022
Given an array A[], the task to find minimum swapping operations required to modify the given array A[] such that for every index in the array, parity(i) = parity(A[i]) where parity(x) = x % 2. If it’s impossible to obtain such an arrangement, then print -1.
Examples:
Input: A[] = { 2, 4, 3, 1, 5, 6 }
Output: 2
Explanation:
Swapping (4, 3) and (5, 6) modifies the array to [2, 3, 4, 1, 6, 5] such that the parity of i and A[i] is same for all indices.
Input: A[] = {1, 2, 5, 7}
Output: -1
Explanation:
The given array cannot be rearranged as per required condition.
Approach:
To solve the problem mentioned above an optimal approach is to choose such an index where parity(i) and parity(A[i]) aren’t the same.
- Initialize two variables needodd and needeven to 0 which will store the parity of each element. Check the parity of index if it’s odd then increase needodd value by 1 otherwise increase needeven.
- If needodd and needeven aren’t same, then required arrangement is not possible.
- Otherwise, the final result is obtained by needodd variable, as it is the number of operations that are required. This is because, at any moment, we choose an odd element whose parity is not the same with parity of their index and similarly choose an even element and swap them.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int parity( int x)
{
return x % 2;
}
int solve( int a[], int size)
{
int needeven = 0;
int needodd = 0;
for ( int i = 0; i < size; i++)
{
if (parity(i) != parity(a[i]))
{
if (parity(i) % 2)
{
needodd++;
}
else
{
needeven++;
}
}
}
if (needeven != needodd)
return -1;
else
return needodd;
}
int main()
{
int a[] = { 2, 4, 3, 1, 5, 6};
int n = sizeof (a) / sizeof (a[0]);
cout << solve(a, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int parity( int x)
{
return x % 2 ;
}
static int solve( int a[], int size)
{
int needeven = 0 ;
int needodd = 0 ;
for ( int i = 0 ; i < size; i++)
{
if (parity(i) != parity(a[i]))
{
if (parity(i) % 2 == 1 )
{
needodd++;
}
else
{
needeven++;
}
}
}
if (needeven != needodd)
return - 1 ;
else
return needodd;
}
public static void main (String[] args)
{
int a[] = { 2 , 4 , 3 , 1 , 5 , 6 };
int n = a.length;
System.out.println(solve(a, n));
}
}
|
Python3
def parity(x):
return x % 2
def solve(a, size):
needeven = 0
needodd = 0
for i in range (size):
if parity(i)! = parity(a[i]):
if parity(i) % 2 :
needodd + = 1
else :
needeven + = 1
if needodd ! = needeven:
return - 1
return needodd
if __name__ = = "__main__" :
a = [ 2 , 4 , 3 , 1 , 5 , 6 ]
n = len (a)
print (solve(a, n))
|
C#
using System;
class GFG{
static int parity( int x)
{
return x % 2;
}
static int solve( int [] a, int size)
{
int needeven = 0;
int needodd = 0;
for ( int i = 0; i < size; i++)
{
if (parity(i) != parity(a[i]))
{
if (parity(i) % 2 == 1)
{
needodd++;
}
else
{
needeven++;
}
}
}
if (needeven != needodd)
return -1;
else
return needodd;
}
public static void Main ()
{
int [] a = {2, 4, 3, 1, 5, 6};
int n = a.Length;
Console.Write(solve(a, n));
}
}
|
Javascript
<script>
function parity(x)
{
return x % 2;
}
function solve(a, size)
{
let needeven = 0;
let needodd = 0;
for (let i = 0; i < size; i++)
{
if (parity(i) != parity(a[i]))
{
if (parity(i) % 2 == 1)
{
needodd++;
}
else
{
needeven++;
}
}
}
if (needeven != needodd)
return -1;
else
return needodd;
}
let a = [ 2, 4, 3, 1, 5, 6 ];
let n = a.length;
document.write(solve(a, n));
</script>
|
Time Complexity: O(n), where n is the size of the given array
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...