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
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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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) |
chevron_right
filter_none
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 */ |
chevron_right
filter_none
Output:
YES
Time Complexity: O(N*M)
Recommended Posts:
- Check if a pair with given product exists in a Matrix
- Check if a pair with given product exists in Linked list
- 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
- Find pair of rows in a binary matrix that has maximum bit difference
- Pair with given product | Set 1 (Find if any pair exists)
- Sort an array according to absolute difference with given value
- Triplets in array with absolute difference less than k
- Find if a binary matrix exists with given row and column sums
- Maximum absolute difference of value and index sums
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Minimum absolute difference of adjacent elements in a circular array
- Check whether (i,j) exists such that arr[i] != arr[j] and arr[arr[i]] is equal to arr[arr[j]]
- Pair with given sum in matrix
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.



