Sir why we are putting -1 even after applying compare

Plz tell logic behind this

@rakesh800650
If u observe that
if u just write
lower_bound(arr,arr+n,key,compare)
without -1 then giving a money value which exists in the array
would return a wrong value
it returns the next greater value in the array

hence the -1 is required so that even 120 and 100 would give us answer 100
in case -1 is not written then 100 would give answer 200 because of the comparator

Sir normally lower bound gives value greater than or equal to and sir in case of 120 it will give ans 200 which is wrong so to get ans we subtract-1 but sir then what work compareter fxn doing here

And sir in compareter which is denoting money a ya b

And sir what will happen if we write a>=b

default comparator for stl lower bound returns a<b it means it rejects the value which are less than searched value and hence will give you index of the value greater than or equal to searched value.
so default lower bound will give 100 as answer of 100 and 200 as answer of 120 but we need 100 as answer of both 100 and 120 so for this we made custom compare function (return a<=b) which rejects the value less than or equal to searched value and will give you the index of next greater than the searched value in this way we will get 200 as answer for 100 as well as for 120 and subtracting 1 from this will give us the correct answer which is 100 for this.

2 Likes

Sir so upper bound fxn will give result right without using comparater

1 Like

yeah sir also said in the video that you can use upper bound here but just to tell how we can made our own custom compare we are using lower bound here.

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.

@sdevwrat this is the best explaination that I have read in money exchanges problems. thnx man

1 Like