title: Largest Rectangle in Histogram using stack
body: <p>Hey friends i am solving this problem but i got an runtime error and not able to figure out that why i am getting this error for the input [0,0]</p>
  <p>my code gives right answer if the input is not [0,0] in 0ms response time..<hr /></p>
  <p>This question is related to the stack data structure. Here is a screenshot and the link of the question..<br>
  <strong>link</strong> - <a href="https://leetcode.com/problems/largest-rectangle-in-histogram/" rel="nofollow noreferrer">https://leetcode.com/problems/largest-rectangle-in-histogram/</a> <br>
  <a href="https://i.stack.imgur.com/IdB2P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IdB2P.png" alt="screenshot1" /></a>
  <a href="https://i.stack.imgur.com/FU5az.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FU5az.png" alt="screenshot2" /></a>
  and here is the error which i get with input [0,0]
  <a href="https://i.stack.imgur.com/4cFFU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4cFFU.png" alt="error screenshot" /></a></p>
  <hr />
  **Here is my code** - 
  <pre><code>class Solution {
  public:
      int largestRectangleArea(vector&lt;int&gt;&amp; h) {
          vector&lt;int&gt; left, right;
          stack&lt;pair&lt;int, int&gt;&gt; l,r;
          //storing left smaller index
          for(int i = 0; i &lt; h.size(); i++){
              if(l.empty()){
                  left.push_back(-1);
                  l.push({h[i], i});
              }else if(l.top().first &lt; h[i]){
                  left.push_back(l.top().second);
                  l.push({h[i], i});
              }else if(!l.empty() &amp;&amp; l.top().first &gt; h[i]){
                  while(!l.empty() &amp;&amp; l.top().first &gt; h[i]){
                      l.pop();
                  }
                  if(l.empty()){
                      left.push_back(-1);
                      l.push({h[i], i});
                  }else{
                      left.push_back(l.top().second);
                      l.push({h[i], i});
                  }
              }
          }
          //storing right smaller index
          for(int i = h.size() - 1; i &gt;= 0; i--){
              if(r.empty()){
                  right.push_back(h.size());
                  r.push({h[i], i});
              }else if(r.top().first &lt; h[i]){
                  right.push_back(r.top().second);
                  r.push({h[i], i});
              }else if(!r.empty() &amp;&amp; r.top().first &gt; h[i]){
                  while(!r.empty() &amp;&amp; r.top().first &gt; h[i]){
                      r.pop();
                  }
                  if(r.empty()){
                      right.push_back(h.size());
                      r.push({h[i], i});
                  }else{
                      right.push_back(r.top().second);
                      r.push({h[i], i});
                  }
              }
          }
          reverse(right.begin(), right.end());
          
          vector&lt;int&gt; ans;
          int find;
          for(int i = 0; i &lt; h.size(); i++){
              find = (right[i] - left[i]) - 1;
              find = find*h[i];
              ans.push_back(find);
          }
          return *max_element(ans.begin(), ans.end());
      }
  };
  </code></pre>