-4

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

enter image description here

acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • It is unclear what you are asking. What _test cases_? What is the specific problem you are facing? – Ron Jun 12 '18 at 11:19
  • Please [learn how to format your posts properly](https://stackoverflow.com/editing-help). – Some programmer dude Jun 12 '18 at 11:21
  • And please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Jun 12 '18 at 11:21
  • And you should probably also [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and [this Stack Overflow question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and all of http://idownvotedbecau.se/ to learn some reasons you get negative votes. – Some programmer dude Jun 12 '18 at 11:23

1 Answers1

2

You are outputting 2 numbers, one immediately after the other:

cout<<a.size();

and

cout<<kthLargest(a,n,k);

The first is 0 (the map is empty), the second is the final result of your function call. The formatting is behaving as expected, and no leading 0 is being output anywhere.

You probably forgot about the cout<<a.size();, so just remove it and your code will be fine.

Baldrick
  • 11,712
  • 2
  • 31
  • 35