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
< ? php function check ( $ n , $ k , & $ a , & $ b ) { sort ( $ a ) ; sort ( $ b ) ; $ fl = False ; $ ind = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] != $ b [ $ i ] ) { if ( $ fl == True ) return False ; $ fl = True ; $ ind = $ i ; } } if ( $ ind == -1 || abs ( $ a [ $ ind ] - $ b [ $ ind ] ) <= $ k ) return True ; return False ; } $ n = 2 ; $ k = 4 ; $ a = array ( 1 , 5 ) ; $ b = array ( 1 , 1 ) ; if ( check ( $ n , $ k , $ a , $ b ) ) echo " Yes " ; else echo " No " ; ? >