Task - Consider an array of n elements. Count the number of pairs that satisfy condition X (sum of the two elements < an arbitrary number, k) .
The basic approach I could think of would be O(n^2), where I evaluate all n*(n-1)/2 pairs. How can I solve the problem in a better time frame i.e., O(n log n) or less?
EDIT: ConsiderThe best approach so far was to sort the problem givenarray, find where a+b smaller than k then add indice of a to solution. But the particular condition Xprocess still has to go through n-1 'b's to check for each of them. It's better but still doesn't improve the worst case.
Also, in this caseisn't homework. I came across this while preparing for a competition.