title: memory limit exceeded in c++
body: <pre><code>int Solution::solve(vector&lt;int&gt; &amp;A)
  {
      sort(A.begin(), A.end());
      map&lt;int, int&gt;ma;
      int m;
      for (int i = 0; i&lt;A.size(); i++)
          ma[A[i]] = 1;
      for (int i = 0; i&lt;A.size(); i++)
          ma[A[i]]++;
      if (A.size() == 1 &amp;&amp; A[0] == 0)
          return 1;
      if (ma[A[0]] == (A.size() + 1))
          return -1;
      for (int i = 0; i&lt;A.size(); i++)
      {
          if (ma[A[i]]&gt;2 &amp;&amp; ma[A[i]]&gt;0)
          {
              m = A.size() - (i + 1) - (ma[A[i]] - 2);
              ma[A[i]] = -1;
  
          }
          if (ma[A[i]] == 2)
              m = A.size() - (i + 1);
          if (m == A[i])
          {
  
              return 1;
          }
      }
      return -1;
  }
  </code></pre>
  
  <p>Given an integer array, find if an integer p exists in the array such that the number of integers greater than p  in the array equals to p
  If such an integer is found return 1 else return -1.
  A is the vector input by the user. </p>
  
  <p>I am writing this program in interviewBit.com
  It is showing <strong>MEMORY LIMIT EXCEEDED</strong> How can I optimise this code, I have tried using array, vector,unordered_map instead of map then it is showing segmentation fault</p>
  
  <p><strong>THE ARRAY CAN HAVE REPEATED ELEMENT AND NEGATIVE INTEGERS ALSO</strong>
  I have used map for keeping track how many times each element is repeated</p>