Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1
Input: nums = [3, 2, 1, 5, 6, 4] and K = 2
Output: 5
Explanation : after sorting above array, it will become [1, 2, 3, 4 , 5 , 6]. So 2nd largest element is 5.
Example 2
Input: nums = [3, 2, 3, 1, 2, 4, 5, 5, 6] and K = 4
Output: 4
Explanation: after sorting above array, it will become [1, 2, 2, 3, 3, 4 , 5, 5, 6]. So 4th largest element is 4.

Note: You can assume k is a valid number, that is 1 <= k <= array.length

using sorting public int findKthLargestUsingSorting(int[] input, int k) { Arrays.sort(input); return input[input.length-k]; }
Complexity Analysis:

Above code runs in O(n log n) time complexity where n is the size of the array. Sorting of the input array Arrays.sort(input) takes O(n log n) time, once the array is sorted then fetching an element using its index takes constant time. Space complexity is constant O(1) as we are not using additional space.

This solution is definitely a overkill to sort entire array for getting just one number.

using Min Heap public int findKthLargestUsingMinHeap(int[] input, int k) { PriorityQueue<Integer> minHeap = new PriorityQueue<>(k); for(int num : input) { minHeap.add(num); if(minHeap.size() > k) { // check when min heap has K+1 th element minHeap.poll(); // remove the top element to ensure it has only K elements } } return minHeap.peek(); // finally return top element, which is nothing but Kth largest }
Complexity Analysis:

Above code runs in O( n log k) time complexity where n is the size of the array and k is the size of Heap.

Min Heap runs a procedure called heapify() to make sure that min element stays at top whenever a new element is added or removed, and this runs in O (log k) time as we are only keeping at most K elements at any given time. This loop is repeated for all elements in the input array, hence O (n log (k)). Space complexity is O(k) as we are using an intermediate data structure Min Heap with size k.

This is better than previous solution if K is reasonably less than N.

using Quickselect algorithm
Let's understand this with an example
Input Array : [3, 2, 1, 9, 8, 5, 6] and K = 2.

Since we have to find 2nd largest element and the input array size is 7, so 2nd largest element is nothing but 5th indexed element after array is sorted.

Step 1: We are going to choose right most element as pivot, that is 6.

You can select any random element as pivot, but there are two popular partition schemes to choose a pivot, Lomuto partition and Hoare partition, in this solution we choose Lomuto partition, so we are going to choose right most element as pivot

Step 2: Using 6 as pivot element, move all elements smaller than 6 to left side and greater than 6 to right side of array using compare and swap technique, so the resultant array looks like this [3,2,1,5, 6 ,9,8].
If you take a closer look at the resultant array after step 2, pivot element is positioned at index 4, which is the correct position when the array is fully sorted.

Step 3: Now, draw one of below conclusions.

After step 3, we can eliminate left partitioned array and just continue above three steps using this new sub array with indexes [pivot_index+1,.....,right].

Complexity Analysis:

QuickSelect algorithm runs in O(n) time complexity in average case where n is the size of the array and O(n2) in the worst case scenario (If numbers are already sorted and pivot selection is bad).
This procedure is an in-place algorithm, so it takes O(1) constant space.

public int quickSelect(int[] nums, int k) { //we are going to find nums.length - k th bigger element from left in the sorting order k = nums.length -k; return select(nums, 0, nums.length-1, k); } public int select(int[] nums, int left, int right, int k) { if(left==right) { // in case nums contains only on element - will return that return nums[left]; } int storeIndex = partition(nums,left,right); if(k == storeIndex) { return nums[k]; } else if (k<storeIndex) { return select(nums, left, storeIndex-1, k); } else { return select(nums, storeIndex+1, right, k); } } public int partition(int[] nums, int left, int right) { int pivotValue = nums[right]; int storeIndex = left; for(int i=left; i<right; i++) { if(nums[i]<pivotValue) { swap(nums, i, storeIndex); storeIndex++; } } swap(nums,right,storeIndex); // move pivot to its final place return storeIndex; } private void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; }
Partition algorithm illustration:

For the illustration purpose, let's take input array as [3, 2, 1, 9, 8, 5, 6] and K = 2. So Kth largest element is nothing but array.length - Kth element when the array is sorted, that is 5th indexed element.

Input Array : Input array [3, 2, 1, 9, 8, 5, 6]

Step 1 is to pick a pivot element, we are choosing right most element, that is pivot = array[right] = 6.

Pivot element selected

Step 2 initialize two variables storeIndex and leftIndex with left index, that is storeIndex = 0, leftIndex = 0 and loop through from left to right-1 and start comparing with pivot.

storeIndex=0 initialization

Once the array goes through method with left and right boundaries as [9,8], it will return answer as 8. That is the 2nd largest element after array is sorted.

Above examples source code can be found at GitHub link for Java code and JUnit tests can be found at GitHub link for Unit tests code