Check if an array can be converted to another given array by swapping pairs of unequal elements | Function to check if arr1 [ ] can be converted to arr2 [ ] by swapping pair ( i , j ) such that i < j and arr [ i ] is 1 and arr [ j ] is 0 ; Stores the differences of prefix sum of arr1 and arr2 ; Stores the count of 1 and zero of arr1 ; Stores the count of 1 and zero of arr2 ; Iterate in the range [ 0 , N - 1 ] ; If arr1 [ i ] is 1 , then increment arr1_one by one ; Otherwise increment arr1_zero by one ; If arr2 [ i ] is 1 , then increment arr2_one by one ; Otherwise increment arr2_zero by one ; Check if number of 1 s and 0 s of arr1 is equal to number of 1 s and 0 s of arr2 respectievly ; Iterate over the range [ 0 , N - 1 ] ; Increment count by differences arr1 [ i ] and arr2 [ i ] ; Check if number of 1 's in  arr2 are more than arr1 and  then print "No" ; Finally , print " Yes " ; Given input arrays ; Size of the array ; Function Call
function canMakeEqual ( arr1 , arr2 , N ) { var count = 0 ; var arr1_one = 0 , arr1_zero = 0 ; var arr2_one = 0 , arr2_zero = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] == 1 ) { arr1_one ++ ; } else if ( arr1 [ i ] == 0 ) { arr1_zero ++ ; } if ( arr2 [ i ] == 1 ) { arr2_one ++ ; } else if ( arr2 [ i ] == 0 ) { arr2_zero ++ ; } } if ( arr1_one != arr2_one arr1_zero != arr2_zero ) { document . write ( " " ) ; return ; } for ( var i = 0 ; i < N ; i ++ ) { count = count + ( arr1 [ i ] - arr2 [ i ] ) ; if ( count < 0 ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } var arr1 = [ 0 , 1 , 1 , 0 ] ; var arr2 = [ 0 , 0 , 1 , 1 ] ; var N = arr1 . length ; canMakeEqual ( arr1 , arr2 , N ) ;