Kth Largest Element in an Array

Medium Solved

Description

Given an integer array nums and an integer k, return the kth largest element in the array.

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

Input format / Clarification:

  • Line 1: JSON array of integers (nums)
  • Line 2: Integer k

Examples

Input:

[3,2,1,5,6,4] 2

Output: 5

Input:

[3,2,3,1,2,4,5,5,6] 4

Output: 4

Note:

Your program must print a single integer representing the kth largest element.

No submissions yet.

Discuss heap-based solutions, quickselect, and time complexity trade-offs.

Test Cases