You are viewing a single comment's thread. Return to all 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; }
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Guys, below is the code in O(n) time complexity and O(1) Auxiliary space