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
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 ) ;