How to do this ? I’m unable to pass all the test cases. Any help will be appreciated!
Here is my code below
Always include a link to the question so that people know it’s not from an ongoing contest and will be much more likely to help!
These kind of posts look very shady if not included with a problem link🤔
Your solution has the complexity O(N*Q) which would result in TLE. As it’s clearly mentioned from the category, you need to use binary search.
Hint
You can use upper_bound and lower_boundI know right
Especially with people not posting the link after informing then and replying with something along the lines of “Trust me, it’s not from an ongoing contest” .
To the poster’s credit , he actually pasted the link so props!
Like @anon73162591 suggested use Binary Search or STL but I’d recommend writing your own functions for upper and lower bound for practise, in contests , save time and use built in. Because unless you understand how the algorithm works , it’s almost of no use knowing the syntax of the function !
In the binary search algo , think of when you should stop and what the values of low, mid and high will be, take a few examples