Loop to iterate over the elements of the matrix
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 ; }