title: Logic used behind Array Manipulation of HackerRank
body: <p>I am not able to grasp the logic behind the solution of this problem of Hackerrank, <a href="https://www.hackerrank.com/challenges/crush/problem" rel="noreferrer">https://www.hackerrank.com/challenges/crush/problem</a></p>
  
  <p>In the discussion section, many have posted their solutions as well but I am unable to understand why that logic works. </p>
  
  <p>The below solution is taken from the discussion section of the same problem and has maximum number of upvotes,</p>
  
  <pre><code>#include &lt;cmath&gt;
  #include &lt;cstdio&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  
  int main() {
      long int N,K,p,q,sum,i,j,max=0,x=0;
  
      cin&gt;&gt;N&gt;&gt;K;
      long int *a=new long int[N+1]();
  
      for(i=0;i&lt;K;i++)
      {
          cin&gt;&gt;p&gt;&gt;q&gt;&gt;sum;
          a[p]+=sum;
          if((q+1)&lt;=N) a[q+1]-=sum;
      }
  
      for(i=1;i&lt;=N;i++)
      {
         x=x+a[i];
         if(max&lt;x) max=x;
  
      }
  
      cout&lt;&lt;max;
      return 0;
  }
  </code></pre>
  
  <p>Could someone please explain the logic behind the same?
  Thanks a ton for your help.</p>