# Maximum litres of water that can be bought with N Rupees

Given Rupees. A liter **plastic bottle** of water costs Rupees and a litre of **glass bottle** of water costs Rupees. But the empty glass bottle after buying can be exchanged for Rupees. Find the maximum liters of water which can be bought with Rupees.

**Examples:**

Input:N = 10 , A = 11 , B = 9 , C = 8

Output:2

One glass bottle can be bought and then can be returned to buy one more glass bottle

Input:N = 15 , A = 6 , B = 4 , C = 3

Output:12

**Approach:** If we have at least money then cost of one glass bottle is **b – c**. This means that if **a ≤ (b – c)** then we don’t need to buy glass bottles, only plastic ones, and the answer will be **floor(n / a)**. Otherwise we need to buy glass bottles while we can.

So, if we have at least money, then we will buy **floor((n – c) / (b – c))** glass bottles and then spend rest of the money on plastic ones.

Below is the implementation of the above approach:

## C++

`// CPP implementation of the above approach ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` ` ` `void` `maxLitres(` `int` `budget,` `int` `plastic,` `int` `glass,` `int` `refund) ` `{ ` ` ` ` ` `// if buying glass bottles is profitable ` ` ` `if` `(glass - refund < plastic) ` ` ` `{ ` ` ` `// Glass bottles that can be bought ` ` ` `int` `ans = max((budget - refund) / (glass - refund), 0); ` ` ` ` ` `// Change budget according the bought bottles ` ` ` `budget -= ans * (glass - refund); ` ` ` ` ` `// Plastic bottles that can be bought ` ` ` `ans += budget / plastic; ` ` ` `cout<<ans<<endl; ` ` ` `} ` ` ` ` ` `// if only plastic bottles need to be bought ` ` ` `else` ` ` `cout<<(budget / plastic)<<endl; ` `} ` ` ` ` ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `budget = 10, plastic=11, glass=9, refund = 8; ` ` ` `maxLitres(budget, plastic, glass, refund); ` `} ` ` ` `// This code is contributed by ` `// Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `class` `GFG ` `{ ` ` ` ` ` `static` `void` `maxLitres(` `int` `budget, ` `int` `plastic, ` ` ` `int` `glass, ` `int` `refund) ` ` ` `{ ` ` ` ` ` `// if buying glass bottles is profitable ` ` ` `if` `(glass - refund < plastic) ` ` ` `{ ` ` ` ` ` `// Glass bottles that can be bought ` ` ` `int` `ans = Math.max((budget - refund) / (glass - refund), ` `0` `); ` ` ` ` ` `// Change budget according the bought bottles ` ` ` `budget -= ans * (glass - refund); ` ` ` ` ` `// Plastic bottles that can be bought ` ` ` `ans += budget / plastic; ` ` ` `System.out.println(ans); ` ` ` `} ` ` ` ` ` `// if only plastic bottles need to be bought ` ` ` `else` ` ` `{ ` ` ` `System.out.println((budget / plastic)); ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `budget = ` `10` `, plastic = ` `11` `, glass = ` `9` `, refund = ` `8` `; ` ` ` `maxLitres(budget, plastic, glass, refund); ` ` ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `def` `maxLitres(budget, plastic, glass, refund): ` ` ` ` ` `# if buying glass bottles is profitable ` ` ` `if` `glass ` `-` `refund < plastic: ` ` ` ` ` `# Glass bottles that can be bought ` ` ` `ans ` `=` `max` `((budget ` `-` `refund) ` `/` `/` `(glass ` `-` `refund), ` `0` `) ` ` ` ` ` `# Change budget according the bought bottles ` ` ` `budget ` `-` `=` `ans ` `*` `(glass ` `-` `refund) ` ` ` ` ` `# Plastic bottles that can be bought ` ` ` `ans ` `+` `=` `budget ` `/` `/` `plastic ` ` ` `print` `(ans) ` ` ` ` ` `# if only plastic bottles need to be bought ` ` ` `else` `: ` ` ` `print` `(budget ` `/` `/` `plastic) ` ` ` `# Driver Code ` `budget, plastic, glass, refund ` `=` `10` `, ` `11` `, ` `9` `, ` `8` `maxLitres(budget, plastic, glass, refund) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `void` `maxLitres(` `int` `budget, ` `int` `plastic, ` ` ` `int` `glass, ` `int` `refund) ` ` ` `{ ` ` ` ` ` `// if buying glass bottles is profitable ` ` ` `if` `(glass - refund < plastic) ` ` ` `{ ` ` ` ` ` `// Glass bottles that can be bought ` ` ` `int` `ans = Math.Max((budget - refund) / (glass - refund), 0); ` ` ` ` ` `// Change budget according the bought bottles ` ` ` `budget -= ans * (glass - refund); ` ` ` ` ` `// Plastic bottles that can be bought ` ` ` `ans += budget / plastic; ` ` ` `Console.WriteLine(ans); ` ` ` `} ` ` ` ` ` `// if only plastic bottles need to be bought ` ` ` `else` ` ` `{ ` ` ` `Console.WriteLine((budget / plastic)); ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `budget = 10, plastic = 11, glass = 9, refund = 8; ` ` ` `maxLitres(budget, plastic, glass, refund); ` ` ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

2

## Recommended Posts:

- Maximum number of candies that can be bought
- Maximum water that can be stored between two buildings
- Buy Maximum Stocks if i stocks can be bought on i-th day
- Water drop problem
- Trapping Rain Water
- Water Connection Problem
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Maximum element in an array such that its previous and next element product is maximum
- Maximum value of |arr[i] - arr[j]| + |i - j|
- Maximum value of arr[i] % arr[j] for a given array
- Maximum even sum subsequence
- Subsequence with maximum odd sum
- Value in a given range with maximum XOR

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.