Program to find maximum how many water bottles we can drink in Python

Suppose there are n number of full water bottles, we can exchange m empty water bottles for only one full water bottle. Now drinking a full water bottle makes it an empty bottle. We have to find the maximum number of water bottles we can drink.

So, if the input is like n = 9, m = 3, then the output will be 13 because initially we have 9 bottles, so after drinking all bottles, we can get 9/3 = 3 full bottles, after drinking them all we have three empty bottles and using them we can buy one and drink it, so we completed 9 + 3 + 1 = 13 bottles.

To solve this, we will follow these steps −

  • x:= n, s:= 0, k:= 0

  • while x >= m, do

    • k:= x mod m

    • x:= quotient of x / m

    • s:= s + x

    • x:= x + k

  • return n + s

Example (Python)

Let us see the following implementation to get better understanding −

 Live Demo

def solve(n, m):
   while x >= m:
      k=x % m
      x=x // m
      s=s + x
      x=x + k
   return n + s

n = 9
m = 3
print(solve(n, m))


9, 3



Updated on: 17-May-2021


Kickstart Your Career

Get certified by completing the course

Get Started