Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1115 |

@bt8816103042 | 739 |

@SherlockHolmes3 | 447 |

@codeantik | 441 |

@SHOAIBVIJAPURE | 430 |

@shalinibhataniya1097 | 408 |

@ShamaKhan1 | 392 |

@neverevergiveup | 381 |

@amrutakashikar2 | 355 |

@mahlawatep | 353 |

@murarry3625 | 352 |

Complete Leaderboard |

You are at the side of a river. You are given a **m** litre jug and a **n** litre jug where **0 < m < n**. Both the jugs are initially empty. The jugs don’t have markings to allow measuring smaller quantities. You have to use the jugs to measure d litres of water where d < n. Determine the minimum no of operations to be performed to obtain d litres of water in one of jug.

The operations you can perform are:

- Empty a Jug
- Fill a Jug
- Pour water from one jug to the other until one of the jugs is either empty or full.

**Example 1:**

Input:m = 3, n = 5, d = 4Output:4Explanation:Operations are as follow- 1. Fill up the 5 litre jug. 2. Then fill up the 3 litre jug using 5 litre jug. Now 5 litre jug contains 2 litre water. 3. Empty the 3 litre jug. 4. Now pour the 2 litre of water from 5 litre jug to 3 litre jug. 5. Now 3 litre jug contains 2 litre of water and 5 litre jug is empty. Now fill up the 5 litre jug. 6. Now fill one litre of water from 5 litre jug to 3 litre jug. Now we have 4 litre water in 5 litre jug.

**Example 2:**

**Input: **m = 8, n = 56, d = 46
**Output: **-1
**Explanation: **Not possible to fill any one of
the jug with 46 litre of water.

**Your Task:**

You don't need to read or print anything. Your task is to complete the function **minSteps()** which takes m, n and d ans input parameter and returns the minimum number of operation to fill d litre of water in any of the two jug.

**Expected Time Comeplxity: **O(d)

**Expected Space Complexity: **O(1)

**Constraints:**

1 ≤ n ≤ m ≤ 100

1 ≤ d ≤ 100

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Two water Jug problem

...