Skip to content

Create 1 June Count pairs Sum in matrices #809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions 1 June Count pairs Sum in matrices
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
int countPairs(vector<vector<int>> &mat1, vector<vector<int>> &mat2, int x) {
int n = mat1.size();
int total = n * n;

int p1 = 0, p2 = total - 1;
int cnt = 0;

while (p1 < total && p2 >= 0) {
int i1 = p1 / n, j1 = p1 % n;
int i2 = p2 / n, j2 = p2 % n;

int sum = mat1[i1][j1] + mat2[i2][j2];

if (sum == x) {
cnt++;
p1++;
p2--;
} else if (sum < x) {
p1++;
} else {
p2--;
}
}

return cnt;
}
};
Loading