In the following program, I have attached code and its sample output I don't know why it is printing 0 as the most significant digit.
I have tried different test cases but it just adds 0 to most significant digit.
Is it a bug or I'm missing some concept.
#include<bits/stdc++.h>
using namespace std;
int kthLargest (vector<int> arr, int n, int k){
map<int,int> a;
cout<<a.size();
vector<int>::iterator i;
for(i=arr.begin();i!=arr.end();i++)
{
a[*i]=a[*i]+1;
}
map<int,int>::iterator j;
int sum=0;
for(j=a.begin();j!=a.end();j++)
{
sum+=j->second;
if(sum>(n-k))
{
sum=j->first;
break;
}
}
return sum;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<int> a(n);
for (int i = 0; i < n; ++i)
{
cin>>a[i];
}
int k;
cin>>k;
cout<<kthLargest(a,n,k);
return 0;
}
input
5
1 2 3 4 5
3
output
04