> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Water and Jug Problem Python Solution

> Tested Python solution for LeetCode 365 with 26 pytest cases. Generate a practice environment with lcpy.

LeetCode 365, [Medium](/catalog/medium). Topics: [Math](/catalog/topics/math), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), Greatest Common Divisor. [View on LeetCode](https://leetcode.com/problems/water-and-jug-problem/description/).

Generate this problem as a practice environment: tested reference solution, 26 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 365   # by problem number
lcpy gen -s water_and_jug_problem   # by problem name
```

## Problem

You are given two jugs with capacities `x` liters and `y` liters. You have an infinite water supply. Return *whether the total amount of water in both jugs may reach* `target` *using the following operations*:

* Fill either jug completely with water.
* Completely empty either jug.
* Pour water from one jug into another until the receiving jug is full, or the transferring jug is empty.

### Examples

```
Input: x = 3, y = 5, target = 4
Output: true
Explanation:
Fill the 5-liter jug (0, 5). Pour from the 5-liter jug into the 3-liter jug, leaving 2 liters (3, 2). Empty the 3-liter jug (0, 2). Transfer the 2 liters from the 5-liter jug to the 3-liter jug (2, 0). Fill the 5-liter jug again (2, 5). Pour from the 5-liter jug into the 3-liter jug until the 3-liter jug is full. This leaves 4 liters in the 5-liter jug (3, 4). Empty the 3-liter jug. Now, you have exactly 4 liters in the 5-liter jug (0, 4).
```

```
Input: x = 2, y = 6, target = 5
Output: false
```

```
Input: x = 1, y = 2, target = 3
Output: true
Explanation: Fill both jugs. The total amount of water in both jugs is equal to 3 now.
```

### Constraints

* `1 <= x, y, target <= 10^3`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/water_and_jug_problem/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/water_and_jug_problem/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from math import gcd


class Solution:
    # Time: O(log(min(x, y)))
    # Space: O(1)
    def can_measure_water(self, x: int, y: int, target: int) -> bool:
        if target > x + y:
            return False
        return target % gcd(x, y) == 0
```

## Complexity

| Time | Space |
| - | - |
| O(log(min(x, y))) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.