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 same.
#arrays #array-rearrange #swap-program