LeetCode The Hard Way

Two Pointers

Two pointers templates for scanning arrays from both ends or maintaining paired indices.

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;
}