find Max fromm k consecutive bags
IntuitionAnalyse that it is better to take start point as either start of interval or end of intervalApproachsort to ensure the intervals are sequential in natu
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Analyse that it is better to take start point as either start of interval or end of interval 
# Approach
<!-- Describe your approach to solving the problem. -->
sort to ensure the intervals are sequential in nature 
sliding window once form i=0 and once from back i=n-1;

# Complexity
- Time complexity:O(nlogn)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# Code
```cpp []
#define ll long long
class Solution {
public:
    long long maximumCoins(vector<vector<int>>& coins, int k) {
        ll n = coins.size();
        sort(coins.begin(), coins.end());
        ll ans = 0, sum = 0;
        
      
        for(ll i = 0, j = 0; i < n; i++) {
            ll endingPoint = coins[i][0] + k - 1;
       
            while(j < n && coins[j][1] <= endingPoint) {
                ll intervalStart = coins[j][0];
                ll intervalEnd = coins[j][1];
                ll score = coins[j][2];
                sum += (intervalEnd - intervalStart + 1) * score;
                j++;
            }
           
            ll partialSum;
            if(j <= n) { 
                if(j < n) {  
                    ll intervalStart = coins[j][0];
                    ll intervalEnd = coins[j][1];
                    ll score = coins[j][2];
                    partialSum = max(0LL, endingPoint - intervalStart + 1) * score;
                    sum += partialSum;
                }
                ans = max(ans, sum);  
                if(j < n) sum -= partialSum;  
            }
       
            ll currentIntervalStart = coins[i][0];
            ll currentIntervalEnd = coins[i][1];
            ll currentIntervalScore = coins[i][2];
            sum -= (currentIntervalEnd - currentIntervalStart + 1) * currentIntervalScore;
        }
        cout<<sum<<endl;
        // Backward pass (same changes as forward pass)
        sum = 0;
        for(ll i = n-1, j = n-1; i >= 0; i--) {
            ll endingPoint = coins[i][1] - k + 1;
            //adding contribution of full interval till its possible to do so
            while(j >= 0 && coins[j][0] > endingPoint) {
                ll intervalStart = coins[j][0];
                ll intervalEnd = coins[j][1];
                ll score = coins[j][2];
                sum += (intervalEnd - intervalStart + 1) * score;
                j--;
            }
             //adding partial sum from the next interval,in case we cant add full interval
            ll partialSum;
            if(j >= -1) { 
                if(j >= 0) { 
                    ll intervalStart = coins[j][0];
                    ll intervalEnd = coins[j][1];
                    ll score = coins[j][2];
                    partialSum = max(0LL, intervalEnd - endingPoint + 1) * score;
                    sum += partialSum;
                }
                ans = max(ans, sum);  
                if(j >= 0) sum -= partialSum; 
            }
            //removing the contribution of current start interval before going to next interval start point
            ll currentIntervalStart = coins[i][0];
            ll currentIntervalEnd = coins[i][1];
            ll currentIntervalScore = coins[i][2];
            sum -= (currentIntervalEnd - currentIntervalStart + 1) * currentIntervalScore;
        }
        return ans;
    }
};
```
['C++']