At time-5:33 cannot understand compare

we have written a compare function which returns a element 100 for 120 but coin 1 is also less than 120
so according to a<=b it should return 1 why 100?

@kartik-malik a<=b will return true for 1, but we actually need the value which returns false
200 is the first value that will return false.
It is essentially behaving like an upper_bound function, ie returns the first value that is strictly greater than the key. I hope that is clear now.

Dont forget to mark your doubt as resolved!

didnt got it 120 please explain step by step

@kartik-malik the array is
{1,2,5,10,20,50,100,200,500,2000} and the key is 120
if we start the traversal from i = 0, and compare each coins[i] with the key
you will see that for the comparison a <= b if we take a = coins[i] and b = key
the first value of a that will return false is 200
all the values before this in coins array will return true.

1 <= 120   True
2 <= 120   True
5 <= 120   True
10 <= 120  True
20 <= 120  True
50 <= 120  True
100 <= 120 True
200 <= 120 False

So the function returns an iterator to 200
Is it clear now?

thanks can u tell what will be returned at each step with inbuilt comparator of lower bound with these inputs

@kartik-malik the inbuilt comparator is a < b i think that was explained in the video
So it will return the first value that is greater than or equal to the key.

Dont forget to mark your doubt as resolved :slight_smile:

but in the video 100 is returned and you are telling address of 200 will be returned

also please tell inbuilt comparator of upperbound

@kartik-malik 200 is returned, we do -1 to get address of 100, please watch the video carefully.

lower_bound: a < b
upper_bound: a <= b

video was confusing thanks

@kartik-malik please mark your doubt as resolved if all your doubts are cleared.

instead of using comparator should we have used upper bound and subtracted 1 from that?

@kartik-malik you can directly use that also, it’d be the same thing. Sir used comparator in this question to explain the concept and usage of comparators.

1 Like

@kartik-malik
u reopened the doubt i hope @Ishitagambhir has cleared all urs doubts…