![[24]7.ai](/_next/image?url=https%3A%2F%2Fstorage.googleapis.com%2Fcompany-logos-bucket%2Flogos%2F247ai.png&w=3840&q=75)
Given two jug capacities jug1 and jug2, and a target amount target, return the minimum number of operations needed to measure exactly target liters. An operation is one of: fill a jug completely, empty a jug, or pour water from one jug to the other until either the source is empty or the destination is full. If it is impossible, return -1.
Example 1:
Input: jug1 = 3, jug2 = 5, target = 4
Output: 6
Explanation: One optimal sequence reaches 4 liters in the 5-liter jug in 6 operations.
Example 2:
Input: jug1 = 2, jug2 = 6, target = 5
Output: -1
Explanation: It is impossible to measure exactly 5 liters using these jug sizes.
1 <= jug1, jug2 <= 10^30 <= target <= jug1 + jug2jug1 = 3, jug2 = 5, target = 4Output6WhyOne optimal sequence is: fill 5, pour to 3, empty 3, pour remaining 2 to 3, fill 5, pour to 3 until full; 4 liters remain in the 5-liter jug.jug1 = 2, jug2 = 6, target = 5Output-1WhyBecause gcd(2, 6) = 2 and 5 is not divisible by 2, measuring exactly 5 liters is impossible.1 <= jug1, jug2 <= 10^30 <= target <= jug1 + jug2Allowed operations: fill, empty, or pour between jugsReturn the minimum number of operations, or -1 if impossibledef min_operations_to_measure(jug1, jug2, target):