Geeksforgeeks

Error

×

Leaderboard

Showing:

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

@Ibrahim Nash | 6564 |

@blackshadows | 6400 |

@mb1973 | 5785 |

@Quandray | 5275 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4639 |

@sanjay05 | 3762 |

@mantu_singh | 3712 |

@gfgaccount | 3627 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@yaswanthero123 | 625 |

@vikash619 | 611 |

@venkat11803610 | 501 |

@roshan13ar | 396 |

@DevangSingh | 379 |

@ravi11815721 | 312 |

@hrishipangavhane | 286 |

@aryanjain85297 | 280 |

@kommaraju11806903 | 277 |

@infosumitraut | 272 |

@ankitsharma5422 | 256 |

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:6Explanation: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?

YesLoading...

Two water Jug problem

...