title: How to find all ordered pairs of elements in array of integers whose sum lies in a given range of value
body: <p>Given an array of integers find the number of all ordered pairs of elements in the array  whose sum lies in a given range [a,b]</p>
  
  <p>Here is an O(n^2) solution for the same </p>
  
  <pre><code>'''
  counts all pairs in array such that the 
  sum of pair lies in the range a and b
  '''
  def countpairs(array, a, b):
      num_of_pairs = 0
      for i in range(len(array)):
          for j in range(i+1,len(array)):
              total = array[i] + array[j]
              if total &gt;= a and total &lt;= b:
                  num_of_pairs += 1
      return num_of_pairs
  </code></pre>
  
  <p>I know my solution is not optimal
  What is a better algorithm for doing this.</p>