• + 346 comments

    Guys, below is the code in O(n) time complexity and O(1) Auxiliary space

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    
    int main() {
        long int N,K,p,q,sum,i,j,max=0,x=0;
    
        cin>>N>>K;
        long int *a=new long int[N+1]();
    
        for(i=0;i<K;i++)
        {
            cin>>p>>q>>sum;
            a[p]+=sum;
            if((q+1)<=N) a[q+1]-=sum;
        }
    
        for(i=1;i<=N;i++)
        {
           x=x+a[i];
           if(max<x) max=x;
    
        }
    
        cout<<max;
        return 0;
    }