What do we need to subtract 1

as we are using compare her
like if
money =1 00 it will check a<=100 and we will get index of 100.
similarly for money =1 20 a<=120 and we will get index of 100.

what do we need to subtract 1 than while finding lb;

#include
#include
using namespace std;
bool compare(int a,int b){
return a<=b;
}
int main() {
int coin[]={1,2,5,10,20,50,100,200,500,1000};
int n = sizeof(coin)/sizeof(coin[0]);
int money;
cin>>money;
int lb = lower_bound(coin,coin+n,money,compare) - coin-1;
cout<<lb<<endl;
cout<<coin[lb];
}

@asifkarim073 try this code without subtracting 1 you will see that for 100 and 120 you are getting index of 200.

Can u tell me what actually compare function is doing here ?

@asifkarim073 when we simply call stl lower_bound without using custom comparator it gives as the index of value which is greater than or equal to the searched value for 100 it will give 100 but for 120 it will give 200 but for money amount 100 and 120 in both case we need change as 100, so here we changed its comparator function as such that it will give as the index of value only greater than searched value not equal to the searched value hence subtracting -1 will always give as the required change of money .

But here in the compare function we are returning a<=b which mean it will give us the index of value smaller or equal to money. and subtracting 1 from that index will give lesser value.
for e.g
if we take money = 120 than compare function will give us the index of value 100 as it is smaller than 120 by condition (a<=b)
and when we subtract 1 from the index of 100 it will give us value 50.

condition a<=b implies that we leave the values which are less than or equal to searched value and hence will return you an value which is not less than or equal to the searched value.