Lower Bound Calculation

Why we are subtracting -1 while calculating variable lb?

Hey @nemishgarg1999
Can you please share the code and comment on the line you need help with.

int lb = lower_bound(coins, coins+n, money, compare) - coins - 1;

Why are we are subtracting -1 as we have already handled this case in compare function?

Bro how can I figure out anything by just looking at one line of code. Please paste the code on CB IDE and then send it to me,

I need to write the whole code manually, or you can see that in video?

Sure send me the position in the video.

17th line at 6:40 of video.

In our custom implementation of compare function, it’ll always return index of first value greater than X. So if you have array = { 1, 2, 4, 7 } and you search for X = 4 or 5 it’ll return index of 7 itself. So you subtract -1 to get index of first value <= X which is index of 4 in our case.

If your doubt is resolved please mark it as cleared.

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.