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:
For example:
x = 3, y = 5, target = 4
should return true
.x = 2, y = 6, target = 5
should return false
.x = 1, y = 2, target = 3
should return true
.Explain your approach and provide the code implementation.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The water and jug problem involves using two jugs of different sizes to measure out a specific amount of water. The brute force method explores all possible combinations of filling, emptying, and transferring water between the jugs. It continues until it either finds a solution or has exhausted every possibility.
Here's how the algorithm would work step-by-step:
def can_measure_water_brute_force(jug_one_capacity, jug_two_capacity, target_capacity):
visited_states = set()
queue = [(0, 0)]
while queue:
current_jug_one, current_jug_two = queue.pop(0)
if current_jug_one == target_capacity or current_jug_two == target_capacity or current_jug_one + current_jug_two == target_capacity:
return True
current_state = (current_jug_one, current_jug_two)
if current_state in visited_states:
continue
visited_states.add(current_state)
# Fill jug one
queue.append((jug_one_capacity, current_jug_two))
# Fill jug two
queue.append((current_jug_one, jug_two_capacity))
# Empty jug one
queue.append((0, current_jug_two))
# Empty jug two
queue.append((current_jug_one, 0))
# Pour jug one into jug two
pour_amount = min(current_jug_one, jug_two_capacity - current_jug_two)
queue.append((current_jug_one - pour_amount, current_jug_two + pour_amount))
# Pour jug two into jug one
pour_amount = min(current_jug_two, jug_one_capacity - current_jug_one)
queue.append((current_jug_one + pour_amount, current_jug_two - pour_amount))
return False
The water and jug problem asks if you can measure a specific amount of water using only two jugs of different sizes. The trick is to realize that the problem can be solved using math focused on the greatest common divisor, avoiding exploring all possible filling and emptying combinations.
Here's how the algorithm would work step-by-step:
def can_measure_water(jug1_capacity, jug2_capacity, target_capacity):
def greatest_common_divisor(first_number, second_number):
while second_number:
first_number, second_number = second_number, first_number % second_number
return first_number
# If the target is larger than the sum, it is impossible.
if target_capacity > jug1_capacity + jug2_capacity:
return False
# Calculate the GCD to determine measurable volumes.
common_divisor = greatest_common_divisor(jug1_capacity, jug2_capacity)
# Check if the target capacity is a multiple of the GCD.
if target_capacity % common_divisor == 0:
return True
else:
return False
Case | How to Handle |
---|---|
Jug1 capacity or Jug2 capacity is zero | If either jug capacity is zero, check if the target capacity is zero or equal to the non-zero capacity. |
Target capacity is greater than the sum of Jug1 and Jug2 capacities | Return false immediately, as it's impossible to measure a capacity larger than the combined jug capacities. |
Jug1 and Jug2 capacities are equal and the target is not a multiple of their capacity. | Return false since the target cannot be measured with the given jug sizes when they are equal and target isn't a multiple. |
Jug1 and Jug2 capacities are relatively prime (GCD is 1) and the target is within the capacity limit. | Return true because any amount within the range can be measured. |
Integer overflow during capacity calculations (sum of jugs or GCD calculations) | Use a wider integer type (e.g., long) or modular arithmetic to prevent overflow during calculations, or check before calculations whether an overflow can occur. |
Target capacity is negative | Return false immediately, as negative capacities are not physically meaningful in this context. |
Both jug capacities are zero, but the target capacity is not zero. | Return false, as it is impossible to measure any non-zero amount with zero capacity jugs. |
The greatest common divisor (GCD) of Jug1 and Jug2 does not divide the target capacity. | Return false, as only multiples of the GCD can be measured; check 'targetCapacity % GCD(jug1Capacity, jug2Capacity) == 0'. |