#include
#include
using namespace std;
bool compare(int a,int b)
{
return a<=b;
}
int main(){
int coins[]={1,2,5,10,20,50,100,200,500,2000};
int n=sizeof(coins)/sizeof(int);
int money=168;
while(money>0)
{
int lb=lower_bound(coins,coins+n,money,compare)-coins-1;
int m=coins[lb];
cout<<m<<",";
money=money-m;
}
}
In the above code why are we subtracting 1 from the lowerbound when we have the comparator returning lower value everytime.
m must become 6-1=5
and m[5]=50 and not 100.
Please explain .