How does the comparator function change the funtionality here?

The lower bound function returns the address of the point where we find a number >=key and we pass a comparator function with compare definition as a<=b wouldn’t the lower bound function return the address of first elements as it is <=key

Hello @mehulbhandari358,

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.

Usually, the inbuilt comparator of lower_bound check for the condition of x<y.
Thus, returning an iterator to the value less than money.
Here, you are checking for equality also.
It is operating the same way binary search works.
It takes two parameters, one of which is money=100(in your example) and compares other elements(at current mid) with 100, in the way binary search work.

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

ir in the vedio the function returns the adress of 200 when we pass the key as 100? if the lower bound function was comparing <key the it would’ve returned 100 which is not the case?

Hello @mehulbhandari358,

The comparator returns the address of 100 only.
But when you subtract coins i.e. the address of first element of the array, it gives the position of 100 in the array i.e. 7.
But, you access the elements with the index at which they are present.
Also, the index begins with 0.
So, the relationship between index and position of an element in an array is, index=position-1
This to get the index of 100 we are then subtracting 1 from the position obtained i.e. 7-1=6.
So, arr[6] is 100 not 200.

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