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
def minimumSwaps ( arr , N ) : NEW_LINE INDENT even , odd = 0 , 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] % 2 != i % 2 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT DEDENT if ( even != odd ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( even ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 7 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE minimumSwaps ( arr , N ) NEW_LINE DEDENT