# 0365. Water and Jug Problem

<https://leetcode.com/problems/water-and-jug-problem>

## Description

You are given two jugs with capacities `jug1Capacity` and `jug2Capacity` liters. There is an infinite amount of water supply available. Determine whether it is possible to measure exactly `targetCapacity` liters using these two jugs.

If `targetCapacity` liters of water are measurable, you must have `targetCapacity` liters of water contained **within one or both buckets** by the end.

Operations allowed:

* Fill any of the jugs with water.
* Empty any of the jugs.
* Pour water from one jug into another till the other jug is completely full, or the first jug itself is empty.

**Example 1:**

```
**Input:** jug1Capacity = 3, jug2Capacity = 5, targetCapacity = 4
**Output:** true
**Explanation:** The famous [Die Hard](https://www.youtube.com/watch?v=BVtQNK_ZUJg&ab_channel=notnek01) example 
```

**Example 2:**

```
**Input:** jug1Capacity = 2, jug2Capacity = 6, targetCapacity = 5
**Output:** false
```

**Example 3:**

```
**Input:** jug1Capacity = 1, jug2Capacity = 2, targetCapacity = 3
**Output:** true
```

**Constraints:**

* `1 <= jug1Capacity, jug2Capacity, targetCapacity <= 106`

## ac

```java
class Solution {
    public boolean canMeasureWater(int x, int y, int z) {
        return z == 0 || x + y >= z && z % gcd(x, y) == 0;
    }

    public int gcd(int x, int y) {
        return y == 0 ? x : gcd(y, x % y);
    }
}

/*
1) bezout's law, ax + by = c has solution then c == gcd(x, y); 2) so just find gcd, return z == 0 || x + y >= z && z % gcd(x, y) == 0;
*/
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jaywin.gitbook.io/leetcode/solutions/0365-water-and-jug-problem.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
