Aloyna is a can lover girl and whenever she goes to a shop, she must buy a can to drink. This time she goes to a shop to buy a can. There are n cans in the shop of k types. The types are numbered from 1 to k. She will buy a specific type of can if the quantity of this type of can is the maximum in the shop. Can you find which type of can Aloyna will buy? For better understanding see the explanation of the sample case.
int n, k;
cin >> n >> k;
int ara[n+1] = {0};
int num;
for (int i = 0; i < n; i++)
{
cin >> num;
ara[num]++;
}
///sort(ara, ara + n);
int save;
int value = -1;
for (int i = 1; i <=n; i++)
if (ara[i] > value)
{
save = i;
value = ara[i];
}
// for(int i=1;i<=n;i++)
// cout<<ara[i]<<" ";
cout << save << endl;
i think this solution should work… i’m using for loop till (n) …but i initialized the whole array with 0…when i made it to go for 1 to k it works …but my question is why this Sol didt work when i went for 1 to n…?
yes there is only k values exist but i made the whole array to 0 ,when i will compare max(ara[i],value) it will cover up the logic …i’m not getting why this is not working!