Why are we subtracting 1

Hey, while calling lower_bound function why are we subtracting 1 ?

@rkyadavr9
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 aray
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 120 because of the comparator

If your query is resolved please close it

1 Like

Hi, Thanks for your reply.
Actually in lower_bound of algorithms … we use comparator as return a<b … I was confused as if it was already return a<=b … because it returns the value greater than or equal to the key … I have read few queries as how it works … it returns the first element not less than the key.

I was confused with comparator.

1 Like

@rkyadavr9
If you haven’t already closed the doubt please close it