I want to be able to find the Kth largest item/element in a subarray.
For example:
list: 3 15 19 7 14
lower bound: 2
upper bound: 4
K: 2
The program would output 15
Example 2:
list: 3 15 19 7 14
lower bound: 1
upper bound: 2
K: 2
The program would output 3
What I've done so far is loop through the subarray itself. I then stored a sorted subarray in another array and located the Nth largest item from there.
int l = 4
int r = 2
int k = 2
int[] temp = new int[1+r-l];
for(int j = l-1; j < r; j++) {
temp[j-l+1] = arr[j];
}
Arrays.sort(temp);
System.out.println(temp[temp.length-k]);
I believe that there is a much faster way of solving this problem.