Minimum number of swaps required to make parity of array elements same as their indices
Given an array arr[] consisting of N integers, the task is to find the minimum number of swaps required to make the parity (i.e., even or odd) of all array elements same as their respective indices. If it is not possible to do so, then print “-1”.
Examples:
Input: arr[] = {3, 2, 7, 6}
Output: 2
Explanation:
Swap {arr[0], arr[1]} and {arr[2], arr[3]}. The array arr[] modifies to {2, 3, 6, 7}.
Now every odd and even element are at odd and even indices respectively.
Therefore, the minimum number of swaps required is 2.
Input: arr[] = {7}
Output: -1
Approach: Follow the steps below to solve the problem:
- Initialize variables even and odd with 0, to store the count of even and odd numbers.
- Traverse the array arr[] using the variable i and perform the following:
- If the parity of arr[i] and i are the same, then continue.
- Check if i is even or not. If found to be true, then update even by incrementing the value by 1.
- Otherwise, update odd by incrementing the value by 1.
- After completing the above steps, if the value of even and odd are not equal, then print -1.
- Otherwise, print the value of even as the minimum number of swaps required.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumSwaps( int arr[], int N)
{
int even = 0, odd = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2 != i % 2) {
if (i % 2 == 0) {
even++;
}
else {
odd++;
}
}
}
if (even != odd) {
cout << -1;
}
else {
cout << even;
}
}
int main()
{
int arr[] = { 3, 2, 7, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
minimumSwaps(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static void minimumSwaps( int arr[], int N)
{
int even = 0 , odd = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] % 2 != i % 2 ) {
if (i % 2 == 0 ) {
even++;
}
else {
odd++;
}
}
}
if (even != odd) {
System.out.println(- 1 );
}
else {
System.out.println(even);
}
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 7 , 6 };
int N = arr.length;
minimumSwaps(arr, N);
}
}
|
Python3
def minimumSwaps(arr, N):
even, odd = 0 , 0
for i in range (N):
if (arr[i] % 2 ! = i % 2 ):
if (i % 2 = = 0 ):
even + = 1
else :
odd + = 1
if (even ! = odd):
print ( - 1 )
else :
print (even)
if __name__ = = '__main__' :
arr = [ 3 , 2 , 7 , 6 ]
N = len (arr)
minimumSwaps(arr, N)
|
C#
using System;
class GFG
{
static void minimumSwaps( int [] arr, int N)
{
int even = 0, odd = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] % 2 != i % 2) {
if (i % 2 == 0) {
even++;
}
else {
odd++;
}
}
}
if (even != odd) {
Console.WriteLine(-1);
}
else {
Console.WriteLine(even);
}
}
public static void Main()
{
int [] arr = { 3, 2, 7, 6 };
int N = arr.Length;
minimumSwaps(arr, N);
}
}
|
Javascript
<script>
function minimumSwaps(arr, N)
{
let even = 0, odd = 0;
for (let i = 0; i < N; i++)
{
if (arr[i] % 2 != i % 2)
{
if (i % 2 == 0)
{
even++;
}
else
{
odd++;
}
}
}
if (even != odd)
{
document.write(-1);
}
else
{
document.write(even);
}
}
let arr = [ 3, 2, 7, 6 ];
let N = arr.length;
minimumSwaps(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
29 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...