Check whether it is possible to make both arrays equal by modifying a single element | Function to check if both sequences can be made equal ; Sorting both the arrays ; Flag to tell if there are more than one mismatch ; To stores the index of mismatched element ; If there is more than one mismatch then return False ; If there is no mismatch or the difference between the mismatching elements is <= k then return true ; Driver code
function check ( n , k , a , b ) { a . sort ( ) ; b . sort ( ) ; let fl = false ; let ind = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { if ( fl == true ) { return false ; } fl = true ; ind = i ; } } if ( ind == - 1 | Math . abs ( a [ ind ] - b [ ind ] ) <= k ) { return true ; } return false ; } let n = 2 , k = 4 ; let a = [ 1 , 5 ] ; let b = [ 1 , 1 ] ; if ( check ( n , k , a , b ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }