Problem overview
A box of candy costs price dollars and you have balance dollars. The method buyCandy returns how many more dollars you need to afford one more box than you can buy now.
With price = 4 and balance = 9, you can buy two boxes, which cost 8 dollars. Three boxes cost 12 dollars, and you have 9, so you need 3 more. The answer is 3.
Approach
The answer comes from three small facts:
- The number of boxes you can already buy is the whole-number part of
balance / price. - One more box means
boxes + 1boxes in total, and their price is(boxes + 1) * price. - The money you are missing is that price minus
balance.
An alternative is a loop that adds one box at a time until the total passes the balance. Arithmetic gets the result directly, with no loop.
Step by step
- Count the whole boxes. Declare
boxesasbalance / price. Both areInt, so Swift divides without keeping a fraction:9 / 4is2. - Price the next count. Declare
balanceNeededas(boxes + 1) * price. The parentheses make the+ 1happen before the multiplication. Without them,boxes + 1 * pricemultiplies first and gives a different number. - Find the gap. Declare
extraBalanceasbalanceNeeded - balance. Because the next box is always more than you have, the gap is positive. - Return the gap. The method promises an
Int, soreturn extraBalancefinishes the job.
The solution in Swift
class Solution {
func buyCandy(_ price: Int, _ balance: Int) -> Int {
let boxes = balance / price
let balanceNeeded = (boxes + 1) * price
let extraBalance = balanceNeeded - balance
return extraBalance
}
}
Swift details worth knowing:
- Integer division truncates. Dividing two
Intvalues gives anIntand drops the fraction, rounding toward zero. For non-negative numbers like these, that is the same as rounding down. letfor values that never change. Each intermediate result is computed once, soletfits. Swift would flag avarthat is never modified.- Type inference. The constants have no written types. Swift infers
Intfrom the parameters. - Division by zero crashes. A
priceof 0 would stop the program atbalance / price, so the lesson assumes a positive price.
Why the answer is never zero
When balance is an exact multiple of price, such as 8 and 4, you can buy exactly 2 boxes with nothing left over. Another box still costs a full 4 dollars, so the answer is 4. The formula handles this on its own: boxes is 2, balanceNeeded is 12, and 12 - 8 is 4.
A shortcut written as price - balance % price gives the same result. It is shorter, but the three named steps show the reasoning.
To check the formula by hand, take price = 5 and balance = 23: boxes is 4, balanceNeeded is 25, and the gap is 2.
Edge cases and common mistakes
- Balance below the price. With
price = 5andbalance = 3,boxesis 0 and the answer is2. - Zero balance. With
price = 4andbalance = 0, the answer is4, a full price. - Missing parentheses.
boxes + 1 * priceis not the cost ofboxes + 1boxes. - Returning
balanceNeeded. That is the total cost, not the missing amount. - Swapped parameters. The order is
pricefirst, thenbalance.
Testing
price = 4,balance = 9gives3.price = 4,balance = 8gives4.price = 4,balance = 0gives4.price = 5,balance = 3gives2.
