Check if two arrays can be made equal by swapping pairs of one of the arrays | Function to check if two arrays can be made equal or not by swapping pairs of only one of the arrays ; Stores elements required to be replaced ; To check if the arrays can be made equal or not ; Traverse the array ; If array elements are not equal ; Increment count by 1 ; Decrement count by 1 ; If flag is true and count is 0 , print " Yes " . Otherwise " No " ; Given arrays ; Size of the array
function checkArrays ( arr1 , arr2 , N ) { let count = 0 ; let flag = true ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { if ( arr1 [ i ] == 0 ) count ++ ; else { count -- ; if ( count < 0 ) { flag = false ; break ; } } } } if ( ( flag && ( count == 0 ) ) == true ) document . write ( " " ) ; else document . write ( " " ) ; } let arr1 = [ 0 , 0 , 1 , 1 ] ; let arr2 = [ 1 , 1 , 0 , 0 ] ; let N = arr1 . length ; checkArrays ( arr1 , arr2 , N ) ;