What is the difference between the below two statements?
i) long long int ar[n+1];
ii) long long int *ar=new long long int[n+1]();
For the array manipulation question in hackerrank, my code failed a few test cases when I used the first statement, but got correct answer with the second statement.
int main()
{
long long int n,m,a,b,k,i,x=0,maxi=INT_MIN;
cin>>n>>m;
//long long int ar[n+1];
long long int *ar=new long long int[n+1]();
while(m--) {
cin>>a>>b>>k;
ar[a]+=k;
if((b+1)<=(n))
ar[b+1]-=k;
}
for(i=1;i<=n;i++) {
x=x+ar[i];
if(maxi<x)
maxi=x;
}
cout<<maxi;
return 0;
}