This repository was archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCount pairs Sum in matrices
More file actions
82 lines (59 loc) · 1.51 KB
/
Copy pathCount pairs Sum in matrices
File metadata and controls
82 lines (59 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
int countPairs(vector<vector<int>> &mat1, vector<vector<int>> &mat2, int n, int x)
{
int count = 0;
// unordered_set 'us' implemented as hash table
unordered_set<int> us;
// insert all the elements of mat2[][] in 'us'
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
us.insert(mat2[i][j]);
// for each element of mat1[][]
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
// if (x-mat1[i][j]) is in 'us'
if (us.find(x - mat1[i][j]) != us.end())
count++;
// required count of pairs
return count;
}
};
// { Driver Code Starts.
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--)
{
int n, x;
cin >> n >> x;
vector<vector<int>> mat1(n, vector<int>(n, -1));
vector<vector<int>> mat2(n, vector<int>(n, -1));
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cin >> mat1[i][j];
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cin >> mat2[i][j];
}
}
Solution ob;
cout << ob.countPairs(mat1, mat2, n, x) << "\n";
}
return 0;
} // } Driver Code Ends