Skip to content

Commit 97d6c68

Browse files
authored
Create 1 June Count pairs Sum in matrices (#809)
2 parents 399e0c0 + 56fca3f commit 97d6c68

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

1 June Count pairs Sum in matrices

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int countPairs(vector<vector<int>> &mat1, vector<vector<int>> &mat2, int x) {
4+
int n = mat1.size();
5+
int total = n * n;
6+
7+
int p1 = 0, p2 = total - 1;
8+
int cnt = 0;
9+
10+
while (p1 < total && p2 >= 0) {
11+
int i1 = p1 / n, j1 = p1 % n;
12+
int i2 = p2 / n, j2 = p2 % n;
13+
14+
int sum = mat1[i1][j1] + mat2[i2][j2];
15+
16+
if (sum == x) {
17+
cnt++;
18+
p1++;
19+
p2--;
20+
} else if (sum < x) {
21+
p1++;
22+
} else {
23+
p2--;
24+
}
25+
}
26+
27+
return cnt;
28+
}
29+
};

0 commit comments

Comments
 (0)