Doubt in my code

Hello, could you please explain me the working of this code ?

I am facing doubt in compare function. I want to know what are the two numbers a and b. I mean what numbers of the coins array it is comparing and also please tell me why we put -1 in statement at line number 16 ?

Hey @yashsharma4304
B is value to be searched and a is array values

For -1 explanation
Indian money exchange problem

Ok thanks for clearing this doubt. But if we are using comparator to make it work like upper bound then simply why don’t we used upper bound?
And as you told there that by using comparator we get the index of searched element which is <= to the key element then if we search for 120 we are already getting 100 then why we do -1 this is my question?

Also I want to know that if we give input as 120 then why we are only getting 100 because 1<=120 , 2<=120, 5<=120, 10<=120, 20<=120 and 50<= 120 ?

So that u learn how to use comparator here.

Go and read that again

Yeah I have given it a read again and now my doubts are cleared alot but there is one more thing I want to ask. When comparator returns true then why lower bound function is not considering that as answer and when it returns false then it accepts that index as answer. Why is it happening ?

You have to look into inbuilt witten code for that , its syntax must be like that.

ok so is it like :

if(!compare){
… //gives the lower bound
}

yeah exactly
image

I found this though comparator function isnt used here but syntax is like that

1 Like

Ok thank you for your great help and support. But I think this behavior of comparator I have only seen in lower bound function because in sort function it was sorting when compare function returns true ?

1 Like

yeah correct …

1 Like

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.