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 without sorting?
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 <= nums.length <= 105
-104 <= nums[i] <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the Kth largest element means going through all possible ways to identify the largest, second largest, all the way to the Kth largest. We essentially simulate ranking everything from scratch. This guarantees we find the right element, but it might take a while.
Here's how the algorithm would work step-by-step:
def find_kth_largest_brute_force(numbers, k_value): working_numbers = numbers[:] # Make a copy to avoid modifying the original input
for _ in range(k_value): # Find the largest element in the current working list largest_index = 0
for current_index in range(1, len(working_numbers)): if working_numbers[current_index] > working_numbers[largest_index]: largest_index = current_index
# Store the largest number to return at the end largest_number = working_numbers[largest_index]
# Remove the largest element from the list. del working_numbers[largest_index]
#After removing k - 1 largest, return the largest return largest_number
To quickly find the kth largest element, we'll use a technique similar to how we sort things, but without sorting the entire list. This approach cleverly partitions the list around a chosen value, allowing us to efficiently narrow down the search area.
Here's how the algorithm would work step-by-step:
def find_kth_largest(numbers, k):
kth_index = len(numbers) - k
def partition(start_index, end_index):
pivot = numbers[end_index]
partition_index = start_index
for i in range(start_index, end_index):
if numbers[i] <= pivot:
numbers[i], numbers[partition_index] = numbers[partition_index], numbers[i]
partition_index += 1
numbers[partition_index], numbers[end_index] = numbers[end_index], numbers[partition_index]
return partition_index
start_index = 0
end_index = len(numbers) - 1
while True:
# Partition the array around a chosen pivot
pivot_index = partition(start_index, end_index)
if pivot_index == kth_index:
# We have found the kth largest element
return numbers[pivot_index]
elif pivot_index < kth_index:
# Search in the right partition
start_index = pivot_index + 1
else:
# Search in the left partition
end_index = pivot_index - 1
Case | How to Handle |
---|---|
nums is null or empty | Return null or throw an IllegalArgumentException since the kth largest element cannot be found. |
k is less than or equal to 0, or k is greater than the length of nums | Throw an IllegalArgumentException as k is outside the valid range of array indices. |
nums contains only one element | Return the only element if k is 1; otherwise, handle as an invalid k value. |
nums contains all identical values | The solution should correctly return the repeated value if k is within the valid range. |
nums contains duplicate values | The solution should correctly identify the kth largest element regardless of duplicate values. |
nums contains very large or very small integers (potential for integer overflow if not handled carefully) | Use appropriate data types (e.g., long) and comparison methods to prevent integer overflow during calculations. |
Large array size; performance considerations (e.g., using a min-heap or quickselect for better time complexity) | Ensure that the algorithm used scales well to large input sizes to avoid exceeding time limits. |
nums contains negative numbers and zeros | The solution should correctly handle both negative numbers and zeros when determining the kth largest element. |