Can you please help me to solve this
Hackerank-data structures
see, while doing this you have to add sum to a[p] and add negative sum at a[q+1]. which make sure that when you add element from a[p] to a[q] sum is added only once and it should be subtracted at a[q+1] as this sum span from p to q only. Rest array element are either 0 or some other input sum. max of addition will be output. refer to below code for p, q, and sum.
#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;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.