Note, please read the bottom of this answer to see why it actually is possible to do the problem. I read the question wrong.
It is not possible in the general case. Consider the list:
n, n-1, n-2 ... 4, 3, 2, 1
The pairs will be:
(n, n-1), (n, n-2) ... (n, 2), (n, 1), (n-1, n-2), (n-1, n-3) ... ... (3, 2), (3, 1), (2, 1)
Hence there are O(n^2) pairs and hence the list cannot be build in O(n log n)
However, you can do this with one pass of the list:
- start at the end of the list and work backwords.
- while moving through the list maintain a heap of the numbers you have seen (this will cause the loop to be O(n log n))
- for ever number you encounter do a search in the heap to find all numbers which are less than the number you are currently on. Output the current number and the number in the heap as a pair. (this is O(n log n) to find the first match in the heap, but will be O(n) to find all smaller numbers)
For your example:
The list: 3 4 1 6 7 3
starting at the second item
heap (3)
item (7)
Output (7, 3)
heap (3, 7)
item (6)
search and find 7, output (6, 3)
heap (3, 6, 7)
item (1)
search and find nothing
heap (1, 3, 6, 7)
item (4)
search and find 3 and 1. output (4, 3) (4, 1)
etc....
Edit, it is possible actually
Since JoshD correctly noted that we are looking for the number of elements, you can use a B-Tree instead of a heap and then you can get just the count of elements less than the current item and add it to a counter.