Start learning free

LESSON 52 OF 338 · CALCULATIONS - CHALLENGES

Buy Candy in Go: Integer Division and the Next Multiple

Practice Read the full explanation

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.
STEP 1 OF 4
    Ctrl + Enter to check

    GO · CALCULATIONS - CHALLENGES

    Buy Candy in Go, explained

    Problem overview

    A box of candy costs price dollars and you have balance dollars. The function buyCandy must return how many more dollars you need to afford one more box than you can afford now.

    Take price = 4 and balance = 9. Two boxes cost 8 dollars, so you can buy two. A third box would bring the total to 12 dollars, and you have 9, so you are 3 dollars short. The answer is 3.

    Approach

    The question is really "what is the next multiple of price above balance, and how far away is it?" There is no need for a loop that buys boxes one at a time. Three small calculations give the answer:

    1. How many whole boxes does balance cover?
    2. What does one more box than that cost in total?
    3. How far is that total from balance?

    Step by step

    1. Count the whole boxes. Divide balance by price. When both operands are integers, Go's / drops the fractional part, which is exactly the number of whole boxes: 9 / 4 is 2. Store it in boxes with :=.
    2. Price one more box. You want boxes + 1 boxes, which cost (boxes + 1) * price. The parentheses make the addition happen before the multiplication. For the example this is 3 * 4 = 12, stored in balanceNeeded.
    3. Find the gap. Subtract the money you have: balanceNeeded - balance is 12 - 9 = 3. Store it in extraBalance.
    4. Return it. The gap is the answer, so return extraBalance.

    The solution in Go

    func buyCandy(price int, balance int) int {
        boxes := balance / price
        balanceNeeded := (boxes + 1) * price
        extraBalance := balanceNeeded - balance
        return extraBalance
    }
    

    Some Go details matter here:

    • Integer division. Because balance and price are both int, the result is an int with the fraction cut off. With non-negative numbers this is the same as rounding down. Go does not turn it into a decimal behind your back.
    • Short declarations. Each := creates a new variable and infers its type, here int, from the right-hand side. Every variable you declare must be used. All three are read by a later line, so the compiler accepts them.
    • Division by zero. A price of 0 would make balance / price panic at run time for integers. A box of candy has a positive price, so the function does not guard against it.

    A shorter route

    The same answer can be found with the remainder operator. balance % price is the money left over after buying as many boxes as possible, and price - balance % price is what that leftover lacks to make one more box. For price = 4 and balance = 9 this is 4 - 1 = 3. The three-variable version in the lesson is longer, but each line has a clear meaning, which makes it easier to check by hand.

    Complexity

    The function does one division, one multiplication, one addition and one subtraction, so it runs in constant time, O(1), and uses constant space.

    Edge cases and common mistakes

    • Balance is an exact multiple of the price. With price = 4 and balance = 8, boxes is 2 and balanceNeeded is 12, so the result is 4, a full price. It is never 0, because you are asked about one more box than you can already afford.
    • Balance smaller than the price. With price = 4 and balance = 3, boxes is 0 and the answer is 4 - 3 = 1.
    • Zero balance. With price = 5 and balance = 0, no boxes and the answer is the full 5.
    • Missing parentheses. boxes + 1 * price multiplies first and gives the wrong total. Use (boxes + 1) * price.
    • Returning the cost instead of the gap. balanceNeeded is the total price, not the shortfall.

    Small inputs worth trying: buyCandy(4, 9) gives 3, buyCandy(4, 8) gives 4, buyCandy(4, 3) gives 1 and buyCandy(5, 0) gives 5.

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free