K'th Largest Number

 Coding Problem Keys 

 K'th Largest Number 

 Problem Statement 

Write a code to find the K'th largest number in a given array. For example, If k = 2, find the second largest number in the given array.

 Input Format 

The first line contains the value of N.
The second line contains the N values of the given array.
The third line contains the value of K.

 Output Format 

Prints the K'th largest number in the given array.

 Sample Input 

n = 6
arr[] = {5, 7, 12, 23, 48, 17}
k = 4

 Sample Output 

12

 Solution 

 Programming Language: Python 3 Language 

n=int(input())
arr=list(map(int,input().split()))
arr.sort()
arr=arr[::-1]
k=int(input())
print(arr[k-1],end=" ")
# Published By PKJCODERS

 Programming Language: C++ Language 

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    int k;
    cin>>k;
    sort(arr,arr+n,greater<int>());
    cout<<arr[k-1];
}
// Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Cut Down & Plant - Trees

Both Adjacent Elements - Odd or Even

Shortest Path in Tree

Sum of Digits - Largest Integer

Unique Element

Number of Profitable Days