- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
def solve(n, m): x=n s=0 k=0 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))
Input
9, 3
Output
13