Top k most frequent number ERROR

I have got one error in code which I can’t debug. Need help!

Hey @Kinjal
You haven’t include header for find its #include
Now try to solve it urself first.

You’re right about not including algorithm header. So, answer is coming wrong. Can you tell?

Hey @Kinjal
First try yourself ,if you are not able to solve it even after trying then I will help you :slight_smile:

Okay bhaiya…

I didn’t see any error so far but I know for sure that something going wrong inside the while loop. Can you give some hint though?

Can u please provide me the algo that u have tried to inplement.

So what I did was like:
Step 1:Use result_vector as a vector where it stores all the elements in order but after doing some operations which I will talk about it in further steps.
Also I have used frequency_map as an unordered map to store and update frequency of all the elements.

Step 2: So the logic is like, whenever I have got a new element in a stream, I will push it to the vector and store its frequency in map.

Step 3: But if the new element is already exist in the vector which is I can confirm by its frequency from unordered_map then,

a.first locate its index in the vector by using find()

b. then try to place it to its correct position inside the vector by using while loop. And this while loop condition will always execute for any element until index = 0.

c. so what we’re doing inside the while loop is that we are comparing the current element to its precedent element but i compare it with two cases:

case 1: if frequency of current element is greater than its precedent element then swap.
case 2: if current element itself is lesser than its precedent element then swap.

Step 4: print k elements from start inside the vector every time.

Step 5: Repeat from Step 2 until loop ends.

Hey @Kinjal
Here error is on line 29
I updated the conditions. https://ide.codingblocks.com/s/330157
while(last_index>0 && ((frequency_map[result_vector[last_index]]>frequency_map[result_vector[last_index-1]] )||( frequency_map[result_vector[last_index]]==frequency_map[result_vector[last_index-1]] &&result_vector[last_index]<result_vector[last_index-1]))){

So bhaiya, is it mean that I have to clarify every little bit of thing in the code like,
Not only current element lesser than its precedent element but also frequency of current element should also be equal to frequency of its precedent element then only we can swap.

But bhaiya, why is that necessary!!! And what if I am not adding this condition (after but also), how is it internally computing with and without it?

PS. I’m not seeing any difference in the dry run but I can see its difference in the compiler by differing in the result.

Hey so the condition above is
Index _is_valid and (if higher freq or same freq but smaller )
if the above condition is true then we swap

If u don’t add the 2nd condition then it will result in false results say
for test case
3 1
2 2 1
Earlier ur vector was forming like this
2
2
1 2
but it should be
2
2
2 1
Because freq of 2nd element is less than 1st element

What I was doing before is like,

Yeah that what I said your condition will sort them in increasing order without considering the freq if smaller number comes later in the stream .
In the above example that I gave say stream is 2 2 2 2 2 2 2 2 1
then for last element 1 will come before 2 in your vector because its smaller but thats not how it should work…

Okay bhaiya, this is a small doubt when I was giving input for this condition,

and input is like:
1
3 2
5 1 3

then output:
5
1 5
3 1

but the correct output:
5
1 5
1 3

why?

I think, I got your point bhaiya. Thanks.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.