Find the Kth Largest Element
Advanced
Let's go on a treasure hunt in a list! Imagine you have a list full of numbers, each representing a kind of treasure. Your task is to find the Kth largest treasure in the list. Isn't that exciting?
In this challenge, you will write a function to find the Kth largest number in a given list of numbers. This will help you practice sorting and indexing in Python!
Function Signature:
def find_kth_largest_element(nums: list[int], k: int) -> int:
Instructions:
- Your function will receive two arguments:
- A list
numsof integers. - An integer
kwhich represents the position from the largest where you will find the treasure, or the number you need.
- A list
- Return the Kth largest number from the list.
Examples:
find_kth_largest_element([3, 1, 2, 1, 5, 4], 2) # returns 4 find_kth_largest_element([7, 10, 4, 3, 20, 15], 3) # returns 10 find_kth_largest_element([2, 1, 2], 1) # returns 2 find_kth_largest_element([2, 1, 2], 2) # returns 2
Tips:
- Remember that in a list sorted in descending order, the largest element is first, the second largest is second, and so on.
- Python's built-in functions might be helpful in sorting the list first!
Have fun, and good luck finding that treasure! 🎉
Inputs
Arg 1:
[3,1,2,1,5,4]Arg 2:
2Expected Output
4Actual Output