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] <= 104When 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:
Imagine you have a big pile of unsorted numbers and you want to find a specific favorite, such as the third largest number. The brute force approach involves looking at every possible way of organizing or comparing the numbers until you are completely sure you have found the exact one you are looking for.
Here's how the algorithm would work step-by-step:
def find_kth_largest(numbers_list, target_rank):
# Sort the entire collection from smallest to largest to establish a clear ordering
sorted_numbers = sorted(numbers_list)
# Count backwards from the largest element to locate the target rank position
target_index = len(sorted_numbers) - target_rank
# Select the specific number at the calculated index as our final answer
return sorted_numbers[target_index]Instead of sorting the entire collection of items, the optimal strategy uses a clever shrinking technique inspired by sorting to narrow down the search. By making strategic guesses and throwing away half of the possibilities that we know cannot be the answer, we quickly hone in on the target item with remarkable speed.
Here's how the algorithm would work step-by-step:
import random
class Solution:
def findKthLargest(self, input_numbers: list[int], target_rank: int) -> int:
target_index = target_rank - 1
left_boundary = 0
right_boundary = len(input_numbers) - 1
while left_boundary <= right_boundary:
pivot_index = random.randint(left_boundary, right_boundary)
pivot_value = input_numbers[pivot_index]
input_numbers[pivot_index], input_numbers[right_boundary] = input_numbers[right_boundary], input_numbers[pivot_index]
store_index = left_boundary
for current_index in range(left_boundary, right_boundary):
if input_numbers[current_index] > pivot_value:
input_numbers[store_index], input_numbers[current_index] = input_numbers[current_index], input_numbers[store_index]
store_index += 1
input_numbers[right_boundary], input_numbers[store_index] = input_numbers[store_index], input_numbers[right_boundary]
# Stop when the pivot lands directly on the target position to avoid unnecessary searching.
if store_index == target_index:
return input_numbers[store_index]
# Narrow the search space to the left partition if the target index is smaller than the pivot.
elif store_index > target_index:
right_boundary = store_index - 1
# Narrow the search space to the right partition if the target index is larger than the pivot.
else:
left_boundary = store_index + 1| Case | How to Handle |
|---|---|
| Array with a single element where k equals 1 | Quickselect or heap approach instantly returns that single element without further iteration. |
| Array containing many duplicate elements and k falling within the duplicate range | Algorithms like Quickselect handle duplicates efficiently through three-way partitioning around the pivot. |
| Array containing negative numbers and mixed signs | Comparison logic correctly evaluates values below zero since the relative ordering of integers remains intact. |
| Extremely large inputs approaching memory limits | An in-place Quickselect approach avoids extra memory allocation while a Min-Heap keeps space complexity bounded by k. |
| k equals the length of the array | The algorithm correctly identifies the minimum element in the array as the kth largest element. |
| Sorted or reverse-sorted input arrays causing worst-case pivot selection | Using a randomized pivot selection prevents worst-case quadratic time complexity in Quickselect. |
| Arrays with all identical values | Partitioning schemes handle identical elements gracefully by grouping them together to terminate early. |