ntree.ai
ntree.aiKth Largest Element

Kth Largest Element

mediumsolvedattempted

You are given an array of integers nums and an integer k.

Return the kth largest element in the array. This is the element in the kth position when the array is sorted from largest to smallest, not the kth distinct value.

Example 1:

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

Example 2:

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

Constraints:

  • 1 <= k <= length of nums <= 10^4
  • -10^4 <= nums[i] <= 10^4
def findKthLargest(nums: list[int], k: int) -> int:
pass
 

Press Visualise to step through your code.