Pair with given sum in matrix
Last Updated :
31 Dec, 2022
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
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++
#include <bits/stdc++.h>
using namespace std;
#define N 4
#define M 4
bool isPairWithSum( int mat[N][M], int sum)
{
unordered_set< int > s;
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 ;
}
int main()
{
int mat[N][M] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int sum = 11;
if (isPairWithSum(mat, sum)) {
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 isPairWithSum( int [][]mat,
int sum)
{
Set<Integer> s = new HashSet<Integer>();
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 ;
}
public static void main(String []args)
{
int [][]mat = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
int sum = 11 ;
if (isPairWithSum(mat, sum))
{
System.out.println( "YES" );
}
else
System.out.println( "NO" );
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static readonly int N = 4;
static readonly int M = 4;
static bool isPairWithSum( int [,]mat,
int sum)
{
HashSet< int > s = new HashSet< int >();
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 ;
}
public static void Main(String []args)
{
int [,]mat = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int sum = 11;
if (isPairWithSum(mat, sum))
{
Console.WriteLine( "YES" );
}
else
Console.WriteLine( "NO" );
}
}
|
Python3
N = 4
M = 4
def isPairWithSum(mat, sum ):
s = set ()
for i in range (N):
for j in range (M):
if ( sum - mat[i][j]) in s :
return True
else :
s.add(mat[i][j])
return False
if __name__ = = '__main__' :
mat = [[ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ] ,
[ 9 , 10 , 11 , 12 ] ,
[ 13 , 14 , 15 , 16 ]]
sum = 11
if (isPairWithSum(mat, sum )) :
print ( "YES" )
else :
print ( "NO" )
|
PHP
<?php
$N = 4;
$M = 4;
function isPairWithSum(& $mat , $sum )
{
global $N , $M ;
$s = array ();
for ( $i = 0; $i < $N ; $i ++) {
for ( $j = 0; $j < $M ; $j ++) {
if (in_array( $sum - $mat [ $i ][ $j ], $s ) != end ( $s )) {
return true;
}
else {
array_push ( $s , $mat [ $i ][ $j ]);
}
}
}
return false;
}
$mat = array ( array ( 1, 2, 3, 4 ),
array ( 5, 6, 7, 8 ),
array ( 9, 10, 11, 12 ),
array (13, 14, 15, 16 ));
$sum = 11;
if (isPairWithSum( $mat , $sum )) {
echo "YES" . "\n" ;
}
else
echo "NO" . "\n" ;
return 0;
?>
|
Javascript
<script>
let N = 4;
let M = 4;
function isPairWithSum(mat,sum)
{
let s = new Set();
for (let i = 0; i < N; i++)
{
for (let j = 0; j < M; j++)
{
if (s.has(sum - mat[i][j]))
{
return true ;
}
else
{
s.add(mat[i][j]);
}
}
}
return false ;
}
let mat = [[ 1, 2, 3, 4 ],
[ 5, 6, 7, 8] ,
[ 9, 10, 11, 12] ,
[13, 14, 15, 16]] ;
let sum = 11;
if (isPairWithSum(mat, sum))
{
document.write( "YES" );
}
else
document.write( "NO" );
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...