human
Minimum number of swaps required to make parity of array elements same as their indices | Function to count the minimum number of swaps required to make the parity of array elements same as their indices.
Stores count of even and odd array elements.
Traverse the array.
Check if indices and array elements are not of the same parity.
If index is even.
Update even.
Update odd.
Condition for not possible.
Otherwise.
Driver Code"
Solution in Python:
assistant
#Here is code to minimum number of swaps required to make parity of array elements same as their indices and the other steps required.
 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 )