Given an NxM matrix and a product K. The task is to check if a pair with the given product 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}};
K = 42
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.
- Start traversing through the matrix, and while traversing check if the current element of the matrix is divisible by the given product and when the product K is divided by the current element, the dividend obtained is also present in the hash.
That is,
(k % mat[i][j] == 0) && (mp[k / mat[i][j]] > 0)
- If present, then return true, else insert current elements 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 isPairWithProductK( int mat[N][M], int k)
{
unordered_set< int > s;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if ((k % mat[i][j] == 0) && (s.find(k / 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 k = 42;
if (isPairWithProductK(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 isPairWithProductK( int mat[][], int k)
{
Set<Integer> s= new HashSet<Integer>();
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < M; j++) {
if ((k % mat[i][j] == 0 ) && s.contains(k / 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 k = 42 ;
if (isPairWithProductK(mat, k)) {
System.out.println( "YES" );
}
else
System.out.println( "NO" );
}
}
|
Python 3
N = 4
M = 4
def isPairWithProductK(mat, k):
s = []
for i in range (N) :
for j in range (M):
if ((k % mat[i][j] = = 0 ) and
(k / / mat[i][j]) in s):
return True
else :
s.append(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 ]]
k = 42
if (isPairWithProductK(mat, k)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static readonly int N = 4;
static readonly int M = 4;
static bool isPairWithProductK( 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 ((k % mat[i, j] == 0) &&
s.Contains(k / mat[i, j]))
{
return true ;
}
else
{
s.Add(mat[i, j]);
}
}
}
return false ;
}
public static void Main()
{
int [,]mat = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int k = 42;
if (isPairWithProductK(mat, k))
{
Console.WriteLine( "YES" );
}
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
let N = 4;
let M = 4;
function isPairWithProductK(mat,k)
{
let s= new Set();
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
if ((k % mat[i][j] == 0) && s.has(Math.floor(k / 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 k = 42;
if (isPairWithProductK(mat, k))
{
document.write( "YES" );
}
else
document.write( "NO" );
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)