Unable to find the error or missing cases

I studied only upto the Stacks and Queues part of the course and tried to solve this question with the things I learnt earlier here. I am unable to understand what 's going wrong with this question.Out of 5 only 1 test case is passing.
Here is the link to my approach:

@code_vishu n is too large 1e7 you can use only upto o(n) time . you are doing in nlogn.
you have to do it in o(n) time using frequency array or map

I can’t understand what are you trying to say?How did you find the time complexity of my code?

@code_vishu there is a loop from i from 0 to n and inside you are using lb and ub which takes logn so n*logn.

Okay.So the problem with it is the Runtime error right?And I need to change my solution.Right?

@code_vishu yes you are right

@code_vishu but your code also gives wrong for some cases
like for
5
1 1 1 1 1
some garbage
5
1 1 1 2 2
it gives 2

Yes for some cases it’s giving some incorrect values.Will try to implement a better solution ahead.Suggested one should be map component right?

@code_vishu yes map or frequency array
but i think freq array will be best

These topics[Freq array,maps,etc] are covered in the C++ STL course provided here.right?Because I haven’t learnt these topics.

map yes but freq array is a basic normal array thing you should already know about it

I haven’t used it properly I guess.It would be helpful if you could share some articles for better insights into freq arrays ?

@code_vishu just study how map is used to calculate freq , then you can automatically use array