Maximize count of empty water bottles from N filled bottles | C ++ program for the above approach ; Function to find the maximum bottles that can be emptied ; Iterate until a is non - zero ; Add the number of bottles that are emptied ; Update a after exchanging empty bottles ; Stores the number of bottles left after the exchange ; Return the answer ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxBottles ( int n , int e ) { int s = 0 , b = 0 ; int a = n ; while ( a != 0 ) { s = s + a ; a = ( a + b ) / e ; b = n - ( a * e ) ; n = a + b ; } return s ; } int main ( ) { int n = 9 , e = 3 ; int s = maxBottles ( n , e ) ; cout << s << endl ; }