Kth Largest Element In Array
Detailed guide and Python implementation for the 'Kth Largest Element In Array' problem.
Problem Statement
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 the sorted order, not the kth distinct element.
Can you solve it in O(n) complexity?
Write a function findKthLargest(nums: List[int], k: int) -> int.
- •1 <= k <= len(nums) <= 10^5
- •-10^4 <= nums[i] <= 10^4
Examples
nums = [3,2,1,5,6,4], k = 2
5
The sorted array is [1,2,3,4,5,6]. The 2nd largest element is 5.
nums = [3,2,3,1,2,4,5,5,6], k = 4
4
The sorted array is [1,2,2,3,3,4,5,5,6]. The 4th largest element is 4.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Try/Except & Error Handling
Prevent your Python scripts from crashing. Learn try, except, finally blocks and how to raise custom exceptions properly.
How to Reverse a String in Python
Learn how to reverse a string in Python using slicing, the reversed() function, and loop concatenation with visual code examples.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.