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:2Explanation:

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:-1Explanation:

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
and**needodd**to**needeven****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++

`// C++ implementation to minimize ` `// swaps required to rearrange ` `// array such that parity of index and ` `// corresponding element is same ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `// Function to return the ` `// parity of number ` `int` `parity(` `int` `x) ` `{ ` ` ` `return` `x % 2; ` ` ` `} ` `// Function to return minimum ` `// number of operations required ` `int` `solve(` `int` `a[], ` `int` `size) ` `{ ` ` ` ` ` `// Initialize needodd and ` ` ` `// needeven value by 0 ` ` ` `int` `needeven = 0; ` ` ` `int` `needodd = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < size; i++) ` ` ` `{ ` ` ` `if` `(parity(i) != parity(a[i])) ` ` ` `{ ` ` ` ` ` `// Check if parity(i) is odd ` ` ` `if` `(parity(i) % 2) ` ` ` `{ ` ` ` ` ` `// increase needodd ` ` ` `// as we need odd no ` ` ` `// at that position. ` ` ` `needodd++; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// increase needeven ` ` ` `// as we need even ` ` ` `// number at that position ` ` ` `needeven++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// If needeven and needodd are unequal ` ` ` `if` `(needeven != needodd) ` ` ` `return` `-1; ` ` ` `else` ` ` `return` `needodd; ` `} ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 2, 4, 3, 1, 5, 6}; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `// Function call ` ` ` `cout << solve(a, n) << endl; ` ` ` `return` `0; ` `} ` `// This code is contributed by venky07 ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to minimize ` `// swaps required to rearrange ` `// array such that parity of index and ` `// corresponding element is same ` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to return the ` `// parity of number ` `static` `int` `parity(` `int` `x) ` `{ ` ` ` `return` `x % ` `2` `; ` `} ` `// Function to return minimum ` `// number of operations required ` `static` `int` `solve(` `int` `a[], ` `int` `size) ` `{ ` ` ` ` ` `// Initialize needodd and ` ` ` `// needeven value by 0 ` ` ` `int` `needeven = ` `0` `; ` ` ` `int` `needodd = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < size; i++) ` ` ` `{` ` ` `if` `(parity(i) != parity(a[i])) ` ` ` `{ ` ` ` ` ` `// Check if parity(i) is odd ` ` ` `if` `(parity(i) % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` ` ` `// Increase needodd ` ` ` `// as we need odd no ` ` ` `// at that position. ` ` ` `needodd++; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// Increase needeven ` ` ` `// as we need even ` ` ` `// number at that position ` ` ` `needeven++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// If needeven and needodd are unequal ` ` ` `if` `(needeven != needodd) ` ` ` `return` `-` `1` `; ` ` ` `else` ` ` `return` `needodd; ` `} ` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `a[] = { ` `2` `, ` `4` `, ` `3` `, ` `1` `, ` `5` `, ` `6` `}; ` ` ` `int` `n = a.length; ` ` ` ` ` `// Function call ` ` ` `System.out.println(solve(a, n)); ` `}` `}` `// This code is contributed by offbeat` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to minimize ` `# swaps required to rearrange ` `# array such that parity of index and ` `# corresponding element is same ` `# Function to return the ` `# parity of number ` `def` `parity(x): ` ` ` `return` `x ` `%` `2` `# Function to return minimum ` `# number of operations required ` `def` `solve(a, size): ` ` ` ` ` `# Initialize needodd and ` ` ` `# needeven value by 0 ` ` ` `needeven ` `=` `0` ` ` `needodd ` `=` `0` ` ` `for` `i ` `in` `range` `(size): ` ` ` ` ` `if` `parity(i)!` `=` `parity(a[i]): ` ` ` ` ` `# Check if parity(i) is odd ` ` ` `if` `parity(i) ` `%` `2` `: ` ` ` ` ` `# increase needodd ` ` ` `# as we need odd no ` ` ` `# at that position. ` ` ` `needodd` `+` `=` `1` ` ` `else` `: ` ` ` `# increase needeven ` ` ` `# as we need even ` ` ` `# number at that position ` ` ` `needeven` `+` `=` `1` ` ` ` ` `# If needeven and needodd are unequal ` ` ` `if` `needodd !` `=` `needeven: ` ` ` `return` `-` `1` ` ` ` ` `return` `needodd ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a ` `=` `[` `2` `, ` `4` `, ` `3` `, ` `1` `, ` `5` `, ` `6` `] ` ` ` `n ` `=` `len` `(a) ` ` ` `print` `(solve(a, n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to minimize ` `// swaps required to rearrange ` `// array such that parity of index and ` `// corresponding element is same ` `using` `System;` `class` `GFG{` ` ` `// Function to return the ` `// parity of number ` `static` `int` `parity(` `int` `x) ` `{ ` ` ` `return` `x % 2; ` `} ` `// Function to return minimum ` `// number of operations required ` `static` `int` `solve(` `int` `[] a, ` `int` `size) ` `{ ` ` ` `// Initialize needodd and ` ` ` `// needeven value by 0 ` ` ` `int` `needeven = 0; ` ` ` `int` `needodd = 0; ` ` ` `for` `(` `int` `i = 0; i < size; i++) ` ` ` `{` ` ` `if` `(parity(i) != parity(a[i])) ` ` ` `{ ` ` ` `// Check if parity(i) is odd ` ` ` `if` `(parity(i) % 2 == 1) ` ` ` `{ ` ` ` `// Increase needodd ` ` ` `// as we need odd no ` ` ` `// at that position. ` ` ` `needodd++; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `// Increase needeven ` ` ` `// as we need even ` ` ` `// number at that position ` ` ` `needeven++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `// If needeven and needodd are unequal ` ` ` `if` `(needeven != needodd) ` ` ` `return` `-1; ` ` ` `else` ` ` `return` `needodd; ` `} ` `// Driver Code` `public` `static` `void` `Main ()` `{` ` ` `int` `[] a = {2, 4, 3, 1, 5, 6}; ` ` ` `int` `n = a.Length; ` ` ` `// Function call ` ` ` `Console.Write(solve(a, n)); ` `}` `}` `// This code is contributed by Chitranayal` |

*chevron_right*

*filter_none*

**Output:**

2

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
- Minimize count of unequal elements at corresponding indices between given arrays
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Minimum swaps to reach permuted array with at most 2 positions left swaps allowed
- Minimize the count of adjacent pairs with different parity
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Maximize count of corresponding same elements in given Arrays by Rotation
- Count of all possible pairs of array elements with same parity
- Rearrange numbers in an array such that no two adjacent numbers are same
- Replace each element of Array with it's corresponding rank
- Check if all the elements can be made of same parity by inverting adjacent elements
- Rearrange an array to minimize sum of product of consecutive pair elements
- Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Minimize operations required to make each element of Array equal to it's index value
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Rearrange an array such that every odd indexed element is greater than it previous
- Construct an array from XOR of all elements of array except element at same index

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.