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:

  1. Your function will receive two arguments:
    • A list nums of integers.
    • An integer k which represents the position from the largest where you will find the treasure, or the number you need.
  2. 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! 🎉

Geek
Inputs

Arg 1:

[3,1,2,1,5,4]

Arg 2:

2
Expected Output
4
Actual Output
Find the Kth Largest Element [advanced] | Coding for Kids