Skip to problem
←

Water Jug Problem Using BFS

Hard 0 attempts

Two unmarked jugs have capacities x and y litres and start empty. There is an unlimited water supply. Each move may:

  • Fill either jug completely.
  • Empty either jug completely.
  • Pour from one jug into the other until the source is empty or the destination is full. You cannot stop a pour arbitrarily.

Use breadth-first search over reachable states to decide whether either jug, or both jugs combined, can hold exactly target litres. Return a boolean; you do not need to return a sequence or a minimum number of moves. target = 0 is already satisfied by the initial state.

For capacities 3 and 5, filling both jugs measures 8 litres in total. To measure 4, one possible sequence of states (amount in first jug, amount in second jug) is:

(0,0) → (0,5) → (3,2) → (0,2) → (2,0) → (2,5) → (3,4)

Function contract

Implement canMeasureWater(x, y, target).

Output: Return true if the target can be held in either jug or as their combined contents; otherwise return false.

Example 1

Input
x = 3
y = 5
target = 4
Output
true

Example 2

Input
x = 2
y = 6
target = 5
Output
false

Example 3

Input
x = 3
y = 5
target = 8
Output
true
Constraints
  • 1 <= x, y <= 10^6
  • 0 <= target <= 10^6
Topics

BFS with compact boundary states

Every move leaves a jug empty or full. Thus every reachable state (a, b) lies on the boundary of 0 ≤ a ≤ x, 0 ≤ b ≤ y; the interior never needs storage.

Assign consecutive IDs to four disjoint groups: a = 0, a = x, then b = 0 and b = y with 0 < a < x. These contain exactly 2(x + y) states. Use a byte array for visited flags and an integer array as a queue. Mark a state when enqueuing it so it enters the queue only once.

For each state, check the target and explore the two fill, two empty, and two maximal-pour moves.

javascript
function canMeasureWater(x, y, target) {
  if (target === 0) return true;
  if (target > x + y) return false;
  const edge = 2 * (y + 1);
  const size = 2 * (x + y);
  const visited = new Uint8Array(size);
  const queue = new Int32Array(size);
  let head = 0, tail = 0;

  function enqueue(a, b) {
    let id;
    if (a === 0) id = b;
    else if (a === x) id = y + 1 + b;
    else id = edge + a - 1 + (b === y ? x - 1 : 0);
    if (!visited[id]) {
      visited[id] = 1;
      queue[tail++] = id;
    }
  }

  enqueue(0, 0);
  while (head < tail) {
    const id = queue[head++];
    let a, b;
    if (id <= y) { a = 0; b = id; }
    else if (id < edge) { a = x; b = id - y - 1; }
    else if (id < edge + x - 1) { a = id - edge + 1; b = 0; }
    else { a = id - edge - x + 2; b = y; }

    if (a === target || b === target || a + b === target) return true;
    enqueue(x, b);
    enqueue(a, y);
    enqueue(0, b);
    enqueue(a, 0);
    const toSecond = Math.min(a, y - b);
    enqueue(a - toSecond, b + toSecond);
    const toFirst = Math.min(b, x - a);
    enqueue(a + toFirst, b - toFirst);
  }
  return false;
}

Time: O(x + y), including array initialization.

Space: O(x + y); the two typed arrays use 10(x + y) bytes, at most 20 MB under the stated bounds.

Loading discussion…

Saved in this browser only. Private to you.

JavaScript