Minimum swaps of same | Function to count the minimum swaps of same - indexed elements from arrays arr1 [ ] and arr2 [ ] required to make the sum of both the arrays even ; Store the sum of elements of the array arr1 and arr2 respectively ; Store the array sum of both the arrays ; If both sumArr1 and sumArr2 are even , prlet 0 and return ; If both sumArr1 and sumArr2 are odd and check for a pair with sum odd sum ; Stores if a pair with odd sum exists or not ; Traverse the array ; If a pair exists with odd sum , set flag = 1 ; Print the answer and return ; For all other cases , prlet - 1 ; Driver Code ; Function Call
function minimumSwaps ( arr1 , arr2 , n ) { let sumArr1 = 0 , sumArr2 = 0 ; for ( let i = 0 ; i < n ; ++ i ) { sumArr1 += arr1 [ i ] ; sumArr2 += arr2 [ i ] ; } if ( sumArr1 % 2 == 0 && sumArr2 % 2 == 0 ) { document . write ( 0 ) ; return ; } if ( sumArr1 % 2 != 0 && sumArr2 % 2 != 0 ) { let flag = - 1 ; for ( let i = 0 ; i < n ; ++ i ) { if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) { flag = 1 ; break ; } } document . write ( flag ) ; return ; } document . write ( - 1 ) ; } let arr1 = [ 11 , 14 , 20 , 2 ] ; let arr2 = [ 5 , 9 , 6 , 3 ] ; let N = arr1 . length ; minimumSwaps ( arr1 , arr2 , N ) ;