Created
May 25, 2020 10:28
-
-
Save cypher-nullbyte/5c64ace638414d2e66284d203b4c3283 to your computer and use it in GitHub Desktop.
Vpropel VIT | POD | 24/05/2020 | Water and Jugs | 34
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Water and Jugs | |
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs. | |
If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end. | |
Operations allowed: | |
Fill any of the jugs completely 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:- | |
Input: x = 3, y = 5, z = 4 | |
Output: Yes | |
Example:- | |
Input: x = 2, y = 6, z = 5 | |
Output: No | |
Input format:- | |
X | |
Y | |
Z | |
Output format:- | |
Yes/No |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<algorithm> | |
using namespace std; | |
/*class Solution | |
{ | |
public: | |
int hcf(int a, int b) | |
{ | |
if(b==0) return a; | |
return hcf(b,a%b); | |
} | |
bool canMeasureWater(int x, int y, int z) | |
{ | |
if(x+y<z) return false; | |
if(z==0) return true; | |
if(x==0&&y==0&&z!=0) return false; | |
if(x==0&&y!=0&&y!=z) return false; | |
if(x==0&&y!=0&&y==z) return true; | |
if(x!=0&&y==0&&x!=z) return false; | |
if(x!=0&&y==0&&x==z) return true; | |
if(x<y) swap(x,y); | |
if(z%hcf(x,y)==0) return true; | |
return false; | |
} | |
}; | |
*/ | |
class Solution | |
{ | |
public: | |
bool canMeasureWater(int x, int y, int z) | |
{ | |
return (z == 0 || x + y >= z && z % __gcd(x, y) == 0); | |
} | |
}; | |
int main() | |
{ | |
int x,y,z; | |
cin>>x>>y>>z; | |
Solution s; | |
if(s.canMeasureWater(x,y,z)) cout<<"Yes"; | |
else cout<<"No"; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arithmatic Approach (to represent with process). Not the solution. | |
#include<iostream> | |
#include<algorithm> | |
int gcd(int a,int b) | |
{ | |
if(b==0) return a; | |
return gcd(b,a%b); | |
} | |
int pour(int fromCap,int toCap,int d) | |
{ | |
int from=fromCap; | |
int to=0; | |
std::cout<<"("<<from<<','<<to<<")\n"; | |
int step=1; | |
while(from!=d && to!=d) | |
{ | |
int temp=std::min(from,toCap-to); | |
to+=temp; | |
from-=temp; | |
std::cout<<"("<<from<<','<<to<<")\n"; | |
step++; | |
if(from==d || to==d) break; | |
if(from==0) | |
{ | |
from=fromCap; | |
std::cout<<"("<<from<<','<<to<<")\n"; | |
step++; | |
} | |
if(to==toCap) | |
{ | |
to=0; | |
std::cout<<"("<<from<<','<<to<<")\n"; | |
step++; | |
} | |
} | |
std::cout<<"\n\n\n"; | |
return step; | |
} | |
int minSteps(int m,int n,int d) | |
{ | |
if(m<n) std::swap(m,n); | |
if(d>n) return -1; | |
if(d%gcd(n,m)!=0) return -1; | |
return std::min(pour(n,m,d),pour(m,n,d)); | |
} | |
int main() | |
{ | |
int n,m,d; | |
std::cin>>n>>m>>d; | |
std::cout<<minSteps(m,n,d); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.geeksforgeeks.org/water-jug-problem-using-memoization/
https://www.geeksforgeeks.org/water-jug-problem-using-bfs/
https://www.geeksforgeeks.org/two-water-jug-puzzle/