Start learning free

LESSON 50 OF 347 · CALCULATIONS - CHALLENGES

Buy Candy in C#: Integer Division and Remainders

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

    C# · CALCULATIONS - CHALLENGES

    Buy Candy in C#, explained

    Problem overview

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

    Take the example price = 4, balance = 9. Two boxes cost 8, so you can buy two and have 1 dollar left. A third box would need 12 dollars in total, which is 3 more than you have. The answer is 3.

    Approach

    The key is knowing how many whole boxes you can already afford. Once you have that count, one more box is that count plus one. Multiply by the price to get the total money required, and subtract what you have.

    The count of whole boxes is the integer part of balance / price. This is a case where C# does what you want by default. When both operands of / are int, the division is an integer division: the fractional part is thrown away, so 9 / 4 is 2, not 2.25. For non-negative numbers this is the same as rounding down.

    Step by step

    1. Count the boxes you can buy. boxes = balance / price gives 2 for the example.
    2. Price one more box. You want boxes + 1 boxes in total, which cost (boxes + 1) * price. The parentheses make the addition happen first. Without them, boxes + 1 * price would add price to boxes, which is a different number. For the example it is (2 + 1) * 4 = 12.
    3. Find the gap. The money still missing is that total minus your current balance: 12 - 9 = 3.
    4. Return it. The method returns the gap, so the caller receives 3.

    The solution in C#

    class Solution {
        public int BuyCandy(int price, int balance) {
            int boxes = balance / price;
            int balanceNeeded = (boxes + 1) * price;
            int extraBalance = balanceNeeded - balance;
            return extraBalance;
        }
    }
    

    Notes on the C# involved:

    • Each int variable is declared and assigned in one line. Naming the intermediate values makes each step easy to read and check.
    • The types matter for the division. If either operand were a double, / would keep the fraction, and the result couldn't be stored in an int without an explicit cast. With two int parameters, no cast is needed.
    • You could write the whole thing in one line, return (balance / price + 1) * price - balance;, but the named steps are clearer for a first time.

    Complexity

    The method does a fixed number of arithmetic operations whatever the input values are. Time is O(1) and extra space is O(1).

    Edge cases and common mistakes

    • An exact multiple. With price = 4 and balance = 8, boxes is 2 and the total for three boxes is 12, so the answer is 4, a full price. It is never 0, because "one more box" always costs something. A solution that computes the remainder and subtracts it from the price would wrongly return 0 here.
    • A balance below one box. With price = 4 and balance = 3, boxes is 0, and the answer is 4 - 3 = 1.
    • A zero balance. BuyCandy(4, 0) returns 4.
    • A price of 0. Dividing an int by zero throws a DivideByZeroException at run time. Prices are positive in this problem, so you don't need to handle it.
    • Forgetting the parentheses. This changes the result, as shown above.

    Testing

    Inputs worth trying, as BuyCandy(price, balance):

    • BuyCandy(4, 9) returns 3.
    • BuyCandy(4, 8) returns 4.
    • BuyCandy(4, 3) returns 1.
    • BuyCandy(4, 0) returns 4.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free