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
# Published By PKJCODERSn=int(input()) arr=list(map(int,input().split())) arr.sort() arr=arr[::-1] k=int(input()) print(arr[k-1],end=" ")
Programming Language: C++ Language
// Published By PKJCODERS#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]; }
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments