Check if a pair with given absolute difference exists in a Matrix | Javascript code to check for pair with given difference exists in the matrix or not ; Function to check if a pair with given difference exists in the matrix ; Store elements in a hash ; Loop to iterate over the elements of the matrix ; Input matrix ; Given difference
let N = 4 ; let M = 4 ; function isPairWithDiff ( mat , k ) { let s = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] > k ) { let m = mat [ i ] [ j ] - k ; if ( s . includes ( m ) ) { return true ; } } else { let m = k - mat [ i ] [ j ] ; if ( s . includes ( m ) ) { return true ; } } s . push ( mat [ i ] [ j ] ) ; } } return false ; } let mat = [ [ 5 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 100 ] ] ; let k = 85 ; document . write ( isPairWithDiff ( mat , k ) == true ? " " : " " ) ;