-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
68 lines (57 loc) · 1.6 KB
/
Copy pathsolution.cpp
File metadata and controls
68 lines (57 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution
{
public:
// Function to calculate GCD
int gcd(int a, int b)
{
while (b != 0)
{
int temp = a % b;
a = b;
b = temp;
}
return a;
}
// Simulate pouring from one jug to another
int pour(int fromCap, int toCap, int d)
{
int from = fromCap; // Fill source jug
int to = 0; // Target jug is empty
int steps = 1; // Filling source jug counts as one step
while (from != d && to != d)
{
// Pour water from source jug to target jug
int transfer = min(from, toCap - to);
to += transfer;
from -= transfer;
steps++;
// Check if we reached target
if (from == d || to == d)
break;
// If source jug becomes empty, fill it again
if (from == 0)
{
from = fromCap;
steps++;
}
// If target jug becomes full, empty it
if (to == toCap)
{
to = 0;
steps++;
}
}
return steps;
}
int minSteps(int m, int n, int d)
{
// If d is greater than both jug capacities
if (d > max(m, n))
return -1;
// If d is not divisible by gcd, it is impossible
if (d % gcd(m, n) != 0)
return -1;
// Try both directions and return minimum
return min(pour(m, n, d), pour(n, m, d));
}
};