# Check if a pair with given absolute difference exists in a Matrix

Given a NxM matrix and a difference K. The task is to check if a pair with the given absolute difference exists in the matrix or not.

Examples:

```Input: mat[N][M] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 100}};
K = 85
Output: YES

Input: mat[N][M] = {{1, 2, 3, 4},
{5, 6, 7, 8}};
K = 150
Output: NO
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Take a hash to store all elements of the matrix in the hash.
• Traverse the matrix and insert all of the elements in a hash.
• Traverse the matrix again and check if (k + current_matrix_element) exists in the hash.
• If present, then return true.
• If all elements of the matrix are traversed and no such pair is found, return false.

Below is the implementation of the above approach:

## C++

 `// CPP code to check for pair with given ` `// difference exists in the matrix or not ` ` `  `#include ` `using` `namespace` `std; ` ` `  `#define N 4 ` `#define M 4 ` ` `  `// Function to check if a pair with given ` `// difference exists in the matrix ` `bool` `isPairWithDiff(``int` `mat[N][M], ``int` `k) ` `{ ` `    ``// Store elements in a hash ` `    ``unordered_set<``int``> s; ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `        ``for` `(``int` `j = 0; j < M; j++)  ` `            ``s.insert(mat[i][j]);        ` `     `  `    ``// looping through elements ` `    ``// if present in the matrix ` `    ``// return true ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `        ``for` `(``int` `j = 0; j < M; j++)  ` `            ``if` `(s.find(k + mat[i][j]) != s.end())  ` `                ``return` `true``;         ` `             `  `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``// Input matrix ` `    ``int` `mat[N][M] = { { 5, 2, 3, 4 }, ` `                      ``{ 5, 6, 7, 8 }, ` `                      ``{ 9, 10, 11, 12 }, ` `                      ``{ 13, 14, 15, 100 } }; ` ` `  `    ``// given difference ` `    ``int` `k = 85; ` ` `  `    ``if` `(isPairWithDiff(mat, k))  ` `        ``cout << ``"YES"` `<< endl;     ` `    ``else` `        ``cout << ``"NO"` `<< endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java code to check for pair with given ` `// difference exists in the matrix or not ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `static` `final` `int` `N = ``4``; ` `static` `final` `int` `M = ``4``; ` ` `  `// Function to check if a pair with given ` `// difference exists in the matrix ` `static` `boolean` `isPairWithDiff(``int` `mat[][], ``int` `k) ` `{ ` `    ``// Store elements in a hash ` `    ``HashSet s = ``new` `HashSet(); ` `    ``for` `(``int` `i = ``0``; i < N; i++)  ` `        ``for` `(``int` `j = ``0``; j < M; j++)  ` `            ``s.add(mat[i][j]);      ` `     `  `    ``// looping through elements ` `    ``// if present in the matrix ` `    ``// return true ` `    ``for` `(``int` `i = ``0``; i < N; i++)  ` `        ``for` `(``int` `j = ``0``; j < M; j++)  ` `                ``if` `(s.contains(k + mat[i][j]) &&  ` `                                ``k + mat[i][j]!=  ` `                                ``(``int``)s.toArray()[ s.size()-``1` `]) ` `                ``return` `true``;      ` `             `  `    ``return` `false``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``// Input matrix ` `    ``int` `mat[][] = { { ``5``, ``2``, ``3``, ``4` `}, ` `                    ``{ ``5``, ``6``, ``7``, ``8` `}, ` `                    ``{ ``9``, ``10``, ``11``, ``12` `}, ` `                    ``{ ``13``, ``14``, ``15``, ``100` `} }; ` ` `  `    ``// given difference ` `    ``int` `k = ``85``; ` ` `  `    ``System.out.println(isPairWithDiff(mat, k) == ``true` `? ``"YES"` `: ``"NO"``);  ` `} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## Python3

 `# Python 3 program to check for pairs  ` `# with given difference exits in the  ` `# matrix or not  ` `N ``=` `4` `M ``=` `4` ` `  `# function to check if a pair with given ` `# difference exist in the matrix ` `def` `isPairWithDiff(mat, k): ` `     `  `    ``# store elements in a hash ` `    ``s ``=` `dict``() ` `     `  `    ``# store elements in dict  ` `    ``for` `i ``in` `range``(N): ` `        ``for` `j ``in` `range``(M): ` `            ``s[mat[i][j]] ``=` `1` `     `  `    ``# looping through elements id present ` `    ``# int the matrix return true ` `    ``for` `i ``in` `range``(N): ` `        ``for` `j ``in` `range``(M): ` `            ``if` `k ``+` `mat[i][j] ``in` `s: ` `                ``return` `True` `     `  `    ``return` `False`         `     `  `# Driver code ` `n, m ``=` `4``, ``4` `mat ``=` `[[``5``, ``2``, ``3``, ``4``], ` `       ``[``5``, ``6``, ``7``, ``8``], ` `       ``[``9``, ``10``, ``11``, ``12``], ` `       ``[``13``, ``14``, ``15``, ``100``]] ` `     `  `# given difference ` `k ``=` `85` ` `  `if` `isPairWithDiff(mat, k): ` `    ``print``(``"Yes"``) ` `else``: ` `    ``print``(``"No"``) ` ` `  `# This code is contributed by ` `# Mohit kumar 29 (IIIT gwalior) `

## C#

 `// C# code to check for pair with given ` `// difference exists in the matrix or not ` `using` `System; ` `using` `System.Collections.Generic;  ` `     `  `class` `GFG  ` `{ ` ` `  `static` `int` `N = 4; ` `static` `int` `M = 4; ` ` `  `// Function to check if a pair with given ` `// difference exists in the matrix ` `static` `Boolean isPairWithDiff(``int` `[,]mat, ``int` `k) ` `{ ` `    ``// Store elements in a hash ` `    ``HashSet<``int``> s = ``new` `HashSet<``int``>(); ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `        ``for` `(``int` `j = 0; j < M; j++)  ` `            ``s.Add(mat[i, j]);      ` `     `  `    ``// looping through elements ` `    ``// if present in the matrix ` `    ``// return true ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `        ``for` `(``int` `j = 0; j < M; j++)  ` `                ``if` `(s.Contains(k + mat[i, j])) ` `                ``return` `true``;      ` `             `  `    ``return` `false``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``// Input matrix ` `    ``int` `[,]mat = { { 5, 2, 3, 4 }, ` `                    ``{ 5, 6, 7, 8 }, ` `                    ``{ 9, 10, 11, 12 }, ` `                    ``{ 13, 14, 15, 100 } }; ` ` `  `    ``// given difference ` `    ``int` `k = 85; ` ` `  `    ``Console.WriteLine(isPairWithDiff(mat, k) == ``true` `? ``"YES"` `: ``"NO"``);  ` `} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

Output:

```YES
```

Time Complexity: O(N*M)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.