in coin change code
int idx=upper_bound(coins,coins+n,money)-1-coins;
why we are subtracting coins for getting index of coins which is just smaller than target
it is giving negative index
in coin change code
int idx=upper_bound(coins,coins+n,money)-1-coins;
why we are subtracting coins for getting index of coins which is just smaller than target
it is giving negative index
Hi @hunt
we are subtracting coins to get index as upperbound returns a pointer at the index so we subtract coins from the pointer to get an int value of index.
i dont think the code will give -ive index since minvalue of money is 1 and we have coin with value 1 so upperboung of 1 will always give 1(index) and idx=upper_bound(coins,coins+n,money)-1-coins dis will give idx=0
still u can share ur code so that i can see where r u facing problem
hope dis helps