I have an Algorithm question.
For example, there is an int[] array like [1,5,4,3,7,2].
I want to find the kth largest difference in this array like :
array[i] - array[j] = kth largest difference
(index i must smaller than j and array[i] must larger than array[j]).
The output is return the j in this question.
My current idea:
- I can build a
int[][]to store all the difference in the array. - Then sorting them and find the kth larget difference.
But time complexity is O(n^2).
Are there better solutions?