215. Kth Largest Element in an Array

Description

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Note: You may assume k is always valid, 1 ≤ k ≤ array's length.

Constraints

Approach

  • GeeksforGeeks

  • ProgramCreek

  • YouTube

Examples

Input: [3, 2, 1, 5, 6, 4] and k = 2

Output: 5

Solutions

/**
 * Time complexity : O(NlogN)
 * Space complexity : O(1)
 */

class Solution {
    public int findKthLargest(int[] nums, int k) {
        Arrays.sort(nums);
        return nums[nums.length-k];
    }
}

Follow up

Last updated

Was this helpful?