Help anyone? where i'm wrong?

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!

1 Like
1 Like

These kind of posts look very shady if not included with a problem link🤔

2 Likes

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_bound
1 Like

I know right :slight_smile:
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” :rofl: .
To the poster’s credit , he actually pasted the link so props!

1 Like

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 :slight_smile:

1 Like