title: Consider an array n of integers A=[a1,a2,a3......an]. Find and print the total number of pairs such that ai*aj <= max(ai,ai+1,.....aj) where i < j
body: <p>Can anyone please help me with the above question.
  We have to find combination of elements in the array (a1,a2),(a1,a3),(a1,a4).... so on, and pick those combinations which satisfies the condition (ai*aj) &lt;= max(A) where A is the array and return the number of combinations possible.</p>
  
  <p>Example : input array A = [1,1,2,4,2] and it returns 8 as the combinations are :
  (1,1),(1,2),(1,4),(1,2),(1,2),(1,4),(1,2),(2,2).</p>
  
  <p>It's easy to solve this using nested for loops but that would be very time consuming.(O(n^2)).</p>
  
  <h1>Naive algorithm:</h1>
  
  <pre><code>array = [1,1,2,4,2]
  result = []
  for i in range(len(array)):
     for j in range(len(array)):
         if array[i] * array[j] &lt;= max(array):
             if (array[j],array[i]) not in result:
                 result.append((array[i],array[j]))
  print(len(result))
  </code></pre>
  
  <p>What should be the approach when we encounter such problems ?</p>