Solved Problem - Kth Largest element
In this lesson, we'll discuss a solved heap problem.
We'll cover the following
Problem statement
Given an array, , consisting of integers; for a given , find the largest element in the array.
Input format
The first line consists of two integers and .
The second line consists of integers representing the array .
Sample
Input 1
5 1
3 6 5 1 4
Output 1
6
Input 2
5 4
3 6 5 1 4
Output 2
3
Explanation
Sample 1: means the largest in the entire array which is 6
.
Sample 2: means the element if you sort the array in non-increasing order. i.e element in [6 5 4 3 1]
, which is 3
.
Brute force
The explanation for sample two suggests a very simple solution:
- Sort the array
- Print the number from right
When sorting, taking time and printing the element is a constant time operation.
Time complexity - .
Get hands-on with 1400+ tech skills courses.