Check if a pair with given absolute difference exists in a Matrix
Given an N×M 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:
- Initialize a hash-map to keep track of already visited elements of the matrix.
- Iterate over the matrix and check if the difference between the current element and k is already present in the hash-map. If yes then the current element and the difference between the elements and k are the desired pair.
- Otherwise, add the current element to the map.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 4
#define M 4
bool isPairWithDiff( int mat[N][M], int k)
{
unordered_set < int > ump ;
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 ( ump.find(m) != ump.end() )
{
return true ;
}
}
else
{
int m = k - mat[i][j] ;
if ( ump.find(m) != ump.end() )
{
return true ;
}
}
ump.insert(mat[i][j]);
}
}
return false ;
}
int main()
{
int mat[N][M] ={ { 5, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 100 } };
int k = 85;
if (isPairWithDiff(mat, k))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static final int N = 4 ;
static final int M = 4 ;
static boolean isPairWithDiff( int mat[][], int k)
{
HashSet<Integer> s = new HashSet<Integer>();
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 ;
}
public static void main(String[] args)
{
int mat[][] = { { 5 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 100 } };
int k = 85 ;
System.out.println(
isPairWithDiff(mat, k) == true ?
"YES" : "NO" );
}
}
|
Python3
N = 4
M = 4
def isPairWithDiff(mat, k):
s = set ()
for i in range (N):
for j in range (M):
if mat[i][j] > k:
m = mat[i][j] - k
if m in s:
return True
else :
m = k - mat[i][j]
if m in s:
return True
s.add(mat[i][j])
return False
n, m = 4 , 4
mat = [[ 5 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 100 ]]
k = 85
if isPairWithDiff(mat, k):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int N = 4;
static int M = 4;
static Boolean isPairWithDiff(
int [,]mat, int k)
{
HashSet< int > s = new 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 ;
}
public static void Main(String[] args)
{
int [,]mat = { { 5, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 100 } };
int k = 85;
Console.WriteLine(
isPairWithDiff(mat, k) == true ?
"YES" : "NO" );
}
}
|
Javascript
<script>
let N = 4;
let M = 4;
function isPairWithDiff(mat,k)
{
let s = [];
for (let i = 0; i < N; i++)
{
for (let j = 0; j < M; j++) {
if ( mat[i][j] > k )
{
let m = mat[i][j] - k ;
if (s.includes(m))
{
return true ;
}
}
else
{
let m = k - mat[i][j] ;
if (s.includes(m))
{
return true ;
}
}
s.push(mat[i][j]);
}
}
return false ;
}
let mat = [[5, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 100]];
let k = 85;
document.write(
isPairWithDiff(mat, k) == true ?
"YES" : "NO" );
</script>
|
Complexity Analysis:
- Time Complexity: O(N*M)
- Auxiliary Space: O(N*M)
Last Updated :
06 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...