Skip to content
BytePatterns

Measure With Two Jugs

MediumMath & Number Theory#gcd#math~25m

Problem

You have two unmarked jugs that hold at most a and b litres, both starting empty, and an unlimited tap. A move fills a jug to the brim, empties a jug completely, or pours one jug into the other until the first is empty or the second is full. Decide whether the two jugs can end up holding exactly t litres between them.

Examples

Input:  a = 3, b = 5, t = 4
Output: True
Why:    one route ends with 4 litres in the big jug and the small jug empty
Input:  a = 2, b = 6, t = 5
Output: False
Why:    every move keeps the total even
Input:  a = 4, b = 6, t = 0
Output: True
Why:    edge case, the jugs already hold zero litres before any move

Hints

0 / 3

Stuck on the idea rather than the code? GCD and Euclid covers it.