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
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 <bits/stdc++.h> 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<Integer> s = new HashSet<Integer>(); 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 = 4M = 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, 4mat = [[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 */ |
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.
Recommended Posts:
- Check if a pair with given product exists in a Matrix
- Check if a pair with given product exists in Linked list
- Longest subsequence such that absolute difference between every pair is atmost 1
- Pair of fibonacci numbers with a given sum and minimum absolute difference
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Pair with maximum difference in a Matrix
- Find pair with maximum difference in any column of a Matrix
- Pair with given product | Set 1 (Find if any pair exists)
- Find pair of rows in a binary matrix that has maximum bit difference
- Find if a binary matrix exists with given row and column sums
- Pair with given sum in matrix
- Pair with maximum sum in a Matrix
- Find a specific pair in Matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Number of pair of positions in matrix which are not accessible
- Check whether (i,j) exists such that arr[i] != arr[j] and arr[arr[i]] is equal to arr[arr[j]]
- Program to check diagonal matrix and scalar matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Triplets in array with absolute difference less than k
- Sort an array according to absolute difference with given value
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.

