I have seen many problems which include counting pairs in an array.
For Example —
You are given an Array with some positive numbers (sorted/unsorted).
You need to find pairs such as (arr[i],arr[j]) , i<j<n with some
property given .
such as arr[i]*2 == arr[j] * 2 , arr[i] ^ arr[j] < arr[j] ^ arr[i] ,
i*arr[i] > j*arr[j] (== or <= or >= any condition ).
I generally solve it using a sorting or hashmap approach for solving these problems. But in some cases, I’m getting TLE.
Anyone has any other ways to solve these problems?. thankx.