Find Positive Integer Solution for a Given Equation LeetCode solution in C++

1
class Solution {
2
public:
3
vector<vector<int>> findSolution(CustomFunction &customfunction, int z) {
4
int i = 1;
5
vector<vector<int>> ans;
6
// finding minimum i such that there is no x>=i ans y>=i for which f(x,y)=z
7
for (; i < 1001; i++) {
8
if (customfunction.f(i, i) > z) {
9
break;
10
}
11
}
12
i--;
13
int k = i;
14
// checking bottom right grid
15
// k represents x and j represents y
16
for (int j = i; j <= 1000 && k > 0; j++) {
17
int curr = customfunction.f(k, j);
18
if (curr == z) {
19
ans.push_back({k, j});
20
k--;
21
} else if (curr > z) {
22
k--;
23
j--;
24
}
25
}
26
k = i;
27
// checking top left grid
28
// now k represents y and j represents x
29
for (int j = i + 1; j <= 1000 && k > 0; j++) {
30
int curr = customfunction.f(j, k);
31
if (curr == z) {
32
ans.push_back({j, k});
33
k--;
34
} else if (curr > z) {
35
k--;
36
j--;
37
}
38
}
39
return ans;
40
}
41
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0