CIW '26 P3 - Gumball Machine
View as PDFCanadian Informatics Workshop 2026: Day 2, Problem 1
Morgan found a mysterious gumball machine under an old tarp in their
attic. The machine has two buttons—one on the left and one on the
right—and two counters labelled and
. Both counters start at
zero. If Morgan presses the left button, counter
increases by one,
and the machine dispenses
gumballs. If Morgan presses the right
button, counter
increases by one, and the machine dispenses
gumballs. The machine may look small, but it contains a seemingly
infinite number of gumballs and will never run out.
Using the fewest button presses, Morgan wants to get exactly
gumballs from the machine in total. Can you help them achieve this?
Input Specification
The first and only line of input contains a single integer,
(
), the number of gumballs Morgan wants to get from
the machine.
The following table shows how the available 25 marks are distributed:
| Marks Awarded | Bounds on |
Additional Constraints |
|---|---|---|
| 7 marks | None | |
| 5 marks | None | |
| 3 marks | The minimum number of button presses can always be achieved by alternating between left and right presses | |
| 10 marks | None |
Output Specification
Output a single integer: the minimum number of button presses Morgan
must perform to dispense exactly gumballs from the machine, or
if it is impossible to do so.
Sample Input
6
Sample Output
5
Explanation for Sample Output
Morgan presses the left button twice, setting to 2 and dispensing no
gumballs. Then they press the right button twice, setting
to 2 and
dispensing 4 gumballs (2 on each press). Finally, they press the left
button once, setting
to 3 and dispensing 2 more gumballs for a total
of 6. It is impossible to dispense exactly 6 gumballs in fewer than 5
presses.
Comments