Find kth max and min element in array in C++
Kth max and min element in array in C++
Here, in this page we will discuss the program to find the kth max and min element in an array in C++ . We use the concept of set in finding the kth maximum and minimum element of the array. We are giving with the size of the array along with array elements and the value of K. We have to print the maximum and the minimum element.
Algorithm :
- Take the size of the array from the user and store it in variable say n.
- Now, declare a vector of size n and take the n elements of the vector say arr from the user.
- Declare one variable say k and take the value of k from the user.
- Now, declare a set say s and insert the vector values in it.
- Declare an iterator say itr and point it to first element of the array and using the advance() function pass itr and k-1 to it, so that it will point to the kth element of the set. (As in set data is stored in sorted manner).
- Now print the value of itr (which reflects the kth minimum value of the vector)
- Again set itr to s.begin().
- Now, pass the itr and n-k to advance() so that it will point to kth maximum value.
Code in C++
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; vectorarr(n); for(int i=0; i<n; i++) cin>>arr[i]; int k; cin>>k; set s(arr.begin(), arr.end()); set::iterator itr = s.begin(); // s.begin() returns a pointer to first element in the set advance(itr, k - 1); //itr points to kth element (minimum)in set cout <<"Minimum :"<< *itr << "\n"; itr = s.begin(); advance(itr, n-k); //itr points to kth element (maximum)in set cout <<"Maximum :"<< *itr << "\n"; return 0; }
Output :
6
80 90 23 45 2 67
4
Minimum :67
Maximum :45
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
void printKthMaxMin(int arr[], int n, int k){
if(k>n) return;
priority_queue <int,vector, greater> p_min(arr,arr+n);
priority_queue p_max(arr,arr+n);
for(int i=0;i<k-1;i++){
p_max.pop();
p_min.pop();
}
cout << p_max.top() << endl;
cout << p_min.top() <> t;
while(t–){
int n;
cin >> n;
int arr[n];
for(int i=0;i> arr[i];
}
int k;
cin >> k;
printKthMaxMin(arr,n,k);
}
}
#include
using namespace std;
int main(){
int n,k;
cin>>n>>k;
vector arr;
int temp;
for(int i=0;i>temp;
arr.push_back(temp);
}
set s(arr.begin(),arr.end());
set::iterator itr=s.begin();
advance(itr,k-1);
cout<<"minimum:"<<*itr<<endl;
itr=s.begin();
advance(itr,n-k);
cout<<"maximum:"<<*itr<<endl;
return 0;
}