2 Sliding windows || O(n) 
IntuitionThe only fear we all have is the window is using partial ranges in left and right. (left and right denoting index of ranges that we are now considering
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The only fear we all have is the window is using partial ranges in left and right. (left and right denoting index of ranges that we are now considering )
i.e some complete ranges and at right and left end we arre having some partial values of left-1 and right+1 ranges .
How optimally find this fractions !!
If you care fully observe then you would see that we will never be having partial left and right simultaneously .
as either left-1 is providing best value or right+1 is providing best value.
so there were 2 time sliding window we need to apply , one for considering left partial and one time right partial 
 


# Approach
<!-- Describe your approach to solving the problem. -->
To solve the problem, we sort the intervals by their start positions, then use a sliding window approach to calculate the maximum coins that can be collected from a sub-interval of size k. For each interval, we consider it as the starting point of a valid 
k-length sub-interval and calculate the total coins from overlapping intervals that fit within this range. We do this process twice: once iterating from left to right and once from right to left, ensuring we consider all possible sub-intervals. The maximum coins collected in either direction is the final answer.

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

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

# Code
```cpp []
#define ll long long
class Solution {
public:
    long long maximumCoins(vector<vector<int>>& coins, int k) {
        sort(coins.begin(),coins.end());
        int n=coins.size();
        ll ans=0;
        for(ll i=0,j=0,sum=0,l=1,r=k+1;j<n;j++){
            l=coins[j][0],r=coins[j][0]+k-1;
            while(i<n&&coins[i][1]<=r){
                sum+=((ll)coins[i][1]-coins[i][0]+1)*coins[i][2];
                i++;
            }
            int buff=0;
            if(i<n){
                if(r>=coins[i][0])buff+=(r-coins[i][0]+1)*coins[i][2];
                
            }
            ans=max(ans,sum+buff);
            sum-=((ll)coins[j][1]-coins[j][0]+1)*coins[j][2];
        }


         for(ll i=n-1,j=n-1,sum=0;j>=0;j--){
          int r=coins[j][1],l=coins[j][1]-k+1;
    
        while(i>=0&&coins[i][0]>=l){
            sum+=((ll)coins[i][1]-coins[i][0]+1)*coins[i][2];
        // cout<<sum<<endl;
            i--;
        }
        int buff=0;
        if(i>=0&&coins[i][1]>=l)buff+=((ll)coins[i][1]-l+1)*coins[i][2];
        // cout<<sum<<" "<<buff<<endl;
ans=max(ans,sum+buff);
sum-=((ll)coins[j][1]-coins[j][0]+ 1)*coins[j][2];
        }
        
        
        return ans;
        
        
    }
};
```
['Two Pointers', 'Sliding Window', 'C++']