📌  相关文章
📜  检查矩阵中是否存在具有给定绝对差的对

📅  最后修改于: 2021-05-08 16:41:44             🧑  作者: Mango

给定一个N×M矩阵和一个差K。任务是检查矩阵中是否存在具有给定绝对差的一对。
例子

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

方法:

  • 初始化哈希图以跟踪矩阵中已访问的元素。
  • 遍历矩阵,并检查当前元素与k的差是否已存在于哈希图中。如果是,则当前元素以及该元素与k的差是所需的对。
  • 否则,将当前元素添加到地图中。

下面是上述方法的实现:

C++
// CPP code to check for pair with given
// difference exists in the matrix or not
 
#include 
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)
{
    unordered_set  ump ;
    
    // Loop to iterate over the matrix
    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;
}
 
// 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 s = new HashSet();
   
    // Loop to iterate over the
    // elements of the matrix
    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;
}
 
// 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 = 4
M = 4
 
# Function to check if a
# pair with given
# difference exist in the matrix
def isPairWithDiff(mat, k):
     
    # Store elements in a hash
    s = set()
     
    # Store elements in dict
    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       
     
# Driver Code
n, m = 4, 4
mat = [[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 s = new HashSet();
    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;
}
 
// 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

时间复杂度:O(N * M)
辅助空间: O(N * M)