http://www.codechef.com/viewsolution/5620359

why is time limit exceeded?

Try using a better sorting technique.Try radix or merge sort. It should work.

See the complexity of your code. Its n^2. The maximum value of n can be 10^9. Thus the maximum number of iterations is square of 10^9. Do you think your code can execute such a large number of iterations in the given time limit? Certainly, No. Therefore, try using a sorting technique with better time complexity. Like Mergesort(nlogn), heapsort, and othersâ€¦