I ran into an interesting algorithm problem:
Given an array of integer, find the number of un-ordered pairs in that array, say given {1, 3, 2}, the answer is 1 becau
It is possible to solve this problem in O(n log n) time using a balanced binary search tree.
Here is a pseudo-code of this algorithm:
tree = an empty balanced binary search tree
answer = 0
for each element in the array:
answer += number of the elements in the tree greater then this element
add this element to the tree