MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / main

Function main

GeeksForGeeks/Kth_Smallest_Element/Solution.cpp:5–38  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3
4
5int main() {
6 int t;
7 cin>>t; //Input the number of test cases
8
9 while(t--){
10 int n;
11 cin>>n; //Input the length of the array entered
12 int a[n];
13 int freq[100001]={0}; //Initializing a frequency array with 0 to keep a count of the frequency of each inputted element
14
15 //Loop to input the elements
16 for(int i=0;i<n;i++){
17 cin>>a[i];
18 freq[a[i]]++; //Increasing the count of the ith element in the frequency array
19 }
20
21 int k;
22 cin>>k;//Input the number k to find the kth smallest element
23
24
25 for(int i=1;i<=100000;i++){
26 if(freq[i]>=1)//To check if the ith element is present in the array by checking its frequency from the frequency array
27 k=k-freq[i];
28
29 //If k becomes 0 or less than 0, it implies that the ith element is the kth smallest element
30 if(k<=0){
31 cout<<i<<endl; //Print the kth smallest element
32 break;
33 }
34 }
35
36 }
37 return 0;
38}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected