
Problem
Solution
Submissions
Kth Largest Element in an Array
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 10
Write a C# program to 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. Implement the FindKthLargest(int[] nums, int k)
function.
Example 1
- Input: nums = [3,2,1,5,6,4], k = 2
- Output: 5
- Explanation:
- Step 1: Sort the array [1,2,3,4,5,6]
- Step 2: The 2nd largest element is 5
Example 2
- Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
- Output: 4
- Explanation:
- Step 1: Sort the array [1,2,2,3,3,4,5,5,6]
- Step 2: The 4th largest element is 4
Constraints
- 1 <= k <= nums.length <= 10^5
- -10^4 <= nums[i] <= 10^4
- Time Complexity: O(n) average case, O(n²) worst case
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- While sorting the array works (O(n log n)), there are more efficient approaches
- Consider using the Quick Select algorithm, which is based on Quick Sort
- Alternatively, a min-heap of size k can be used
- Focus on partitioning the array and only recursing on the side that contains the kth largest element
- Remember that the kth largest is at index n-k in a sorted array