when we are passing compare function in lower bound with <= function then why we are subtracting 1 in getting the desired index
for example in indian money change video
lb = lower_bound(coins, coins+n, money, compare) - coins - 1 ;
why we need -1 when we are already passed compare function as less than or equal to
Lower bound with comparision
Hey @code_beater
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.