Find Kth Largest Element
Find kth largest element in array.
AdvancedPhase 4 - Level 5: Logical & AppliedExample 11 of 14
find-kth-largest-element.py
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67k = int(input("Enter k: "))89# Sort in descending order10arr_sorted = sorted(arr, reverse=True)1112if k <= len(arr_sorted):13 print(f"{k}th largest: {arr_sorted[k-1]}")14else:15 print("Invalid k")
Output
Enter array size: 5 Element 1: 10 Element 2: 5 Element 3: 20 Element 4: 15 Element 5: 8 Enter k: 2 2th largest: 15
What's going on
Sort array and find kth element.
Key Concepts:
Sort array in descending order
Access kth element (index k-1)
Handle invalid k
🔗Related Content
- 📖
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- 📝
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- 💻
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum