O(n) and O(1) space 2 sliding windows solution in 1 pass
IntuitionThe problem gives the hint of sliding window based on that once {l,r} is sorted, any range that lies beyond r, will not have any index to the currentLe
# Intuition
The problem gives the hint of sliding window based on that once {l,r} is sorted, any range that lies beyond r, will not have any index to the currentLeft, where currentLeft is the leftmost index one can pick to satisfy the range till r.

# Approach
In addition to the above sliding window idea, we need to consider that we can have the current range in 2 ways
1. Have the range starting from l and extending till l + k - 1
2. Have the range ending at r and starting from r - k + 1

Thus, we need 2 sliding windows, one to capture the ranges starting from l, the other to capture ranges ending at l.

# Complexity
- Time complexity:
**O(n)**

- Space complexity:
**O(1)**

# Code
```cpp []
typedef long long ll;

class Solution {
public:
    long long maximumCoins(vector<vector<int>>& coins, int k) {
        ll res = 0;
        sort(coins.begin(), coins.end());

        ll right = 0, left = 0;
        ll curr = 0;
        ll curright = 0;
        ll total = 0;

        for(ll i = 0; i < coins.size(); i++){
            ll l = coins[i][0], r = coins[i][1];
            ll rightBoundary = l + k - 1, leftBoundary = r - k + 1;
            total += (r - l + 1LL) * coins[i][2];

            right = max(i, right);
            while(right < coins.size() && rightBoundary >= coins[right][0]){
                if(rightBoundary <= coins[right][1]){
                    ll val = (rightBoundary - coins[right][0] + 1) * coins[right][2];
                    res = max(res, curright + val);
                    break;
                }
                else
                    curright += (1LL * coins[right][1] - coins[right][0] + 1) * coins[right][2];
                right++;
                res = max(res, curright);
            }

            while(leftBoundary > coins[left][1]){
                total -= (coins[left][1] - coins[left][0] + 1LL) * coins[left][2];
                left++;
            }
            if(coins[left][0] <= leftBoundary){
                ll val = (leftBoundary - coins[left][0]) * coins[left][2];
                res = max(res, total - val);
            }
            else
                res = max(res, total);
            //cout << curright << " " << total << endl;
            if(rightBoundary > r)
                curright -= (r - l + 1) * coins[i][2];
        }
        return res;
    }
};
```
['C++']