title: Rainwater harvesting problem for a series of walls and facing an issue on some large test cases of wrong answer 130/150 passed
body: <p>I'm getting a wrong answer on finding the output for a huge input test case. I have thought the logic I have used to be appropriate. Please let me know what is the issue in this code ?</p>
  <p>Some input examples to understand the logic are</p>
  <ul>
  <li>Input</li>
  </ul>
  <pre><code>N = 4
  arr[] = {7,4,0,9}
  </code></pre>
  <ul>
  <li>Output</li>
  </ul>
  <pre><code>10
  </code></pre>
  <ul>
  <li><p>Explanation:<br>
  Water trapped by above
  block of height 4 is 3 units and above
  block of height 0 is 7 units. So, the
  total unit of water trapped is 10 units.</p>
  </li>
  <li><p>Input</p>
  </li>
  </ul>
  <pre><code>N = 3
  arr[] = {6,9,9}
  </code></pre>
  <ul>
  <li>Output</li>
  </ul>
  <pre><code>0
  </code></pre>
  <ul>
  <li>Explanation:<br>
  No water will be trapped.</li>
  </ul>
  <pre><code>long long trappingWater(int arr[], int n) {
    // code here
    vector&lt;pair&lt;int, int&gt;&gt; v;
    long long count = 0;
    for (int i = 0; i &lt; n; i++) {
      if (arr[i] != 0)
        v.push_back({arr[i], i});
    }
    int z = v.size();
    for (int i = z - 1; i &gt;= 1; i--) {
      int end_index = v[i].second;
      int end_wall = v[i].first;
  
      int maxer = 0;
      for (int j = i - 1; j &gt;= 0; j--) {
        int start_index = v[j].second;
        int start_wall = v[j].first;
        long long temp = 0;
        if (end_index - start_index &gt;= 2)
          temp =
              (end_index - start_index - 1) * (min(end_wall, start_wall) - maxer);
  
        if (temp &gt; 0)
          count += temp;
  
        maxer = max(start_wall, maxer);
  
        if (start_wall &gt;= end_wall)
          break;
      }
      // cout&lt;&lt;count&lt;&lt;endl;
    }
    return count;
  }
  
  </code></pre>
  <p>this is the question link run/submit with this one <a href="https://practice.geeksforgeeks.org/problems/trapping-rain-water-1587115621/1#" rel="nofollow noreferrer">https://practice.geeksforgeeks.org/problems/trapping-rain-water-1587115621/1#</a></p>