Check if a pair with given absolute difference exists in a Matrix | C # 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 ; Driver Code ; Input matrix ; Given difference
using System ; using System . Collections . Generic ; class GFG { static int N = 4 ; static int M = 4 ; static Boolean isPairWithDiff ( int [ , ] mat , int k ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i , j ] > k ) { int m = mat [ i , j ] - k ; if ( s . Contains ( m ) ) { return true ; } } else { int m = k - mat [ i , j ] ; if ( s . Contains ( m ) ) { return true ; } } s . Add ( mat [ i , j ] ) ; } } return false ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 5 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 100 } } ; int k = 85 ; Console . WriteLine ( isPairWithDiff ( mat , k ) == true ? " YES " : " NO " ) ; } }