# Pair with given sum in matrix

Given a NxM matrix and a sum S. The task is to check if a pair with given Sum 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, 16}};
sum = 31
Output: YES

Input: mat[N][M] = {{1, 2, 3, 4},
{5, 6, 7, 8}};
sum = 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.
• Start traversing through the matrix, and while traversing check if abs(sum-matrix_element) is present in the hash.
• If present, then return true, else insert the current matrix element into the hash.
• If all elements of the matrix are traversed and no pair is found, return false.

Below is the implementation of the above approach:

## C++

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

## Java

 `// Java code to check for pair ` `// with given sum ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to check if a pair with  ` `// given sum exists in the matrix ` `static` `final` `int` `N = ``4``; ` `static` `final` `int` `M = ``4``; ` `static` `boolean` `isPairWithSum(``int` `[][]mat,  ` `                             ``int` `sum) ` `{ ` `    ``// hash to store elements ` `    ``Set s = ``new` `HashSet(); ` ` `  `    ``// looping through elements ` `    ``// if present in the matrix ` `    ``// return true, else push ` `    ``// the element in matrix ` `    ``for` `(``int` `i = ``0``; i < N; i++)  ` `    ``{ ` `        ``for` `(``int` `j = ``0``; j < M; j++) ` `        ``{ ` `            ``if` `(s.contains(sum - mat[i][j]))  ` `            ``{ ` `                ``return` `true``; ` `            ``} ` `            ``else`  `            ``{ ` `                ``s.add(mat[i][j]); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``return` `false``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` `  `    ``// Input matrix ` `    ``int` `[][]mat = { { ``1``, ``2``, ``3``, ``4` `}, ` `                    ``{ ``5``, ``6``, ``7``, ``8` `}, ` `                    ``{ ``9``, ``10``, ``11``, ``12` `}, ` `                    ``{ ``13``, ``14``, ``15``, ``16` `} }; ` ` `  `    ``// given sum ` `    ``int` `sum = ``11``; ` ` `  `    ``if` `(isPairWithSum(mat, sum))  ` `    ``{ ` `        ``System.out.println(``"YES"``); ` `    ``} ` `    ``else` `        ``System.out.println(``"NO"``); ` `} ` `} ` ` `  `// This code is contributed by ihritik  `

## C#

 `// C# code to check for pair ` `// with given sum ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to check if a pair with  ` `// given sum exists in the matrix ` `static` `readonly` `int` `N = 4; ` `static` `readonly` `int` `M = 4; ` `static` `bool` `isPairWithSum(``int` `[,]mat,  ` `                            ``int` `sum) ` `{ ` `    ``// hash to store elements ` `    ``HashSet<``int``> s = ``new` `HashSet<``int``>(); ` ` `  `    ``// looping through elements ` `    ``// if present in the matrix ` `    ``// return true, else push ` `    ``// the element in matrix ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `    ``{ ` `        ``for` `(``int` `j = 0; j < M; j++) ` `        ``{ ` `            ``if` `(s.Contains(sum - mat[i, j]))  ` `            ``{ ` `                ``return` `true``; ` `            ``} ` `            ``else` `            ``{ ` `                ``s.Add(mat[i, j]); ` `            ``} ` `        ``} ` `    ``} ` `    ``return` `false``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` `  `    ``// Input matrix ` `    ``int` `[,]mat = { { 1, 2, 3, 4 }, ` `                    ``{ 5, 6, 7, 8 }, ` `                    ``{ 9, 10, 11, 12 }, ` `                    ``{ 13, 14, 15, 16 } }; ` ` `  `    ``// given sum ` `    ``int` `sum = 11; ` ` `  `    ``if` `(isPairWithSum(mat, sum))  ` `    ``{ ` `        ``Console.WriteLine(``"YES"``); ` `    ``} ` `    ``else` `        ``Console.WriteLine(``"NO"``); ` `} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## PHP

 ` `

Output:

```YES
```

Time Complexity: O(N*M)

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.

Improved By : ihritik, chitranayal, Rajput-Ji

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.