Money change(stl)


whats wrong in this code?

@tishya_goyal, your logic is not correct ,as the lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than val.
so if you pass 168 in your lower bound function then it will return iterator to 200 , since 200 is the first value not less than 168 , and since you subtract 1 from the index you will get a[res] as 100,
but if you have 200 , then you will get iterator for 200 and when you subtract 1 from the index you will get index for 100 which should not be the case as 200 can be exchanged for 1 note of 200

so what you can do is pass comparator function to your lower bound function , so what happens with comparator function is the function will return the index for first element which comparator function returns false.

So, the comparator used here is :-

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

b will be the key and a will be the elements of the array

you can refer to this discussion for some more info on comparator function :-

I have corrected your code here :-

In case of any doubt feel free to ask :slight_smile:

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.