Indian money change

In the question of Indian money change,
I couldn’t understand what is the role of compare function whose syntax is as follows:
bool compare(int a, int b)
{
return a<=b;
}
Please explain this in detail.
Also please explain does the same compare function works inside the sort(arr,arr+n,compare). Please explain the working of this function in detail

Hello @Tiwary725,

To understand the requirement of this comparator you need to understand the lower_bound() function.

It returns an iterator pointing to the first element in the range [first,last) which does not compare less than val i.e. just smaller than the passed value.

But, in this question we want a coin that is either equal to the amount or just smaller(the closet) to it.

Thus, to consider this equal to, sir has written an external comparator.

Hope, this would help.
Give a like if you are satisfied.

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.

#include
#include

using namespace std;

//USING BINARY SEARCH WITH COMPARATORS.

bool compare(int a,int b)
{
return a<=b;
}

int main()
{
//int n;
//cin>>n;

int coins[] = {1,2,5,10,20,50,100,200,500,2000};
int n = sizeof(coins)/sizeof(int);
//int coins[n];
int money = 100;

/*for(int i=0;i<n;i++)
{
    cin>>coins[i];
}*/

/*while(money>0)
{

}*/

//USING UPPER BOUND. DON'T NEED OF COMPARATOR.

/*auto lb = upper_bound(coins,coins+n,money);
cout<<lb<<endl;
cout<<(lb-coins)<<endl;
//cout<<coins[lb]<<endl;*/


while(money>0)
{
    auto lb = lower_bound(coins,coins+n,money,compare);
    cout<<lb<<endl;
    cout<<(lb-coins)<<endl;
    //cout<<coins[lb]<<endl;
    auto m = coins[lb];
    cout<<m<<",";
    money = money-m;
}

}

I am dealing with error here, whether I need to hold address from lower bound by using (int*) instead of auto?.