Indian money change problem

in this problem, we re using lower_bound function with a comparator which says it should return a<=b,so , what is the use of subtracting 1 from it to get lb(lower bound) if we are alreay getting one number smaller?

hello @Suruchi

if we use lower bound then not everytime we need to subtract -1 (if key is already present in that case -1 is not required) so to make it uniform we have converted its behaviour to upper bound to always get correct result.

so,can we directly use upper bound function?

yeah…u can

i tried it it’s not working.
see, when we find lower bound of an element which is already present , it will return true at that index, right, so in that case also , we r subtracting 1 , but it’s working right. why ?

#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;
}
}
plse explain this code.
image

PLEASE HELP! I AM CONFUSED ABOUT THIS TOO
What is the use of -1 if key==element in coins array