Two Pointers
- C++
- Java
int f(vector<int>& v) {
int ans = 0;
int l = 0, r = (int) v.size() - 1;
while (l < r) {
// do some logic here
if (/* condition */) {
l += 1;
} else {
r -= 1;
}
}
return ans;
}
int f(int[] v) {
int ans = 0;
int l = 0, r = v.length - 1;
while (l < r) {
// do some logic here
if (/* condition */) {
l += 1;
} else {
r -= 1;
}
}
return ans;
}