C++ || Sorting + Sliding Window 
Complexity
Time complexity: O(N∗Log(N))


O(N∗Log(N)) For sorting




O(3∗N) For calculating maximum coins
Total : O(N∗Log(N))+O(3∗N)≈O(N∗Log(N))



Space compl
# Complexity
- Time complexity: $$O(N*Log(N))$$
- - $$O(N*Log(N))$$ For sorting
- - $$O(3*N)$$ For calculating maximum coins
   Total : $$O(N*Log(N)) + O(3*N) ≈ O(N*Log(N)) $$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    void removeFromBegining(vector<vector<int>>& coins, int& sI,
                            long long& curCoins, int& k, int& i) {
        if (sI == i) {
            curCoins = 0;
            sI++;
            i++;
            return;
        }
        curCoins -= (long long)(coins[sI][1] - coins[sI][0] + 1) * coins[sI][2];
        sI++;
    }
    long long helper(vector<vector<int>>& coins, int k) {
        int startIndex = 0;
        long long curCoins = 0;
        long long ans = 0;
        int i = 0;
        while (i < coins.size()) {
            int s = coins[i][0];
            int e = coins[i][1];
            int limit = coins[startIndex][0] + k;
            
            if (e <= limit) {
            // If the current range is fully within limit, include it entirely 
                curCoins += ((long long)(e - s + 1) * coins[i][2]);
                ans = max(ans, curCoins);
                i++;
            } else {
            // If the current range exceeds the limit, take part of it
                ans = max(ans, curCoins + (long long)max(0, limit - s + 1) * coins[i][2]);
            // remove range from the begining 
                removeFromBegining(coins, startIndex, curCoins, k, i);
            }
        }
        return ans;
    }
    long long maximumCoins(vector<vector<int>>& coins, int k) {
        k--;
        sort(coins.begin(), coins.end());
        // Calculate maximum coins from left to right
        long long ans = helper(coins, k);
        // reverse the coins array and range for each coins array element 
        reverse(coins.begin(), coins.end());
        int maxi = coins[0][1];
        for (int i = 0; i < coins.size(); i++) {
            int a = maxi - coins[i][0];
            int b = maxi - coins[i][1];
            coins[i][0] = b;
            coins[i][1] = a;
        }
        // Calcuate again maximum coins with the reversed range
        ans = max(ans, helper(coins, k));
        return ans;
    }
};
```
['Array', 'Greedy', 'Sliding Window', 'Sorting', 'Prefix Sum', 'C++']