A box of candy costs price dollars. You have balance dollars. Compute the number of boxes of candy you can buy and return how many more dollars you need to buy one more box of candy.
Example:
Input: price = 4, balance = 9 Output: 3 Explanation: You can buy two boxes of candy that costs 4 * 2 = 8 dollars. If you would have 12 dollars you could buy 3 boxes (one more), so you would need 3 more dollars.Hints:
The number of boxes we can buy is the integer part of the division of
balance by price. Let's store this number in a variable boxes.
We want to buy one more box, so a total of
boxes + 1 boxes, which require (boxes + 1) * price dollars. Let's store this product into another variable balanceNeeded.
Finally the answer is the difference between
balanceNeeded and balance.
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check