title: How to find number of elements greater/smaller than an element in an array?
body: <p>I want to find the fastest way to find the number of elements which are smaller/greater than each element in an array.</p>
  
  <p><strong>For example</strong> : Array is [5, 6, 8, 2, 4]. Considering first element, no of elements smaller than it is 2.</p>
  
  <p>The best I could make myself was that each element be compared with the rest of the array elements but it takes a long time for large arrays with number of entries approx 10^5.</p>
  
  <p>My code:</p>
  
  <pre><code>for(i=0;i&lt;n;i++)
  {
      count=0;
      for(j=0;j&lt;n;j++)
      {
          if( i!=j &amp;&amp; (ar[i]&gt;ar[j]) )
          {
              count++;
          }
      }
      printf("%lld ",count);
  }
  </code></pre>
  
  <p><strong>Edit:</strong> I want to display the number of elements smaller than each array element. That is for the above example, I want the answer to be : 2 3 4 0 1 
  And the <strong>array can contain repeated values.</strong></p>