- Related Questions & Answers
- Maximum number of candies that can be bought in C
- Program to find maximum number of package that can be bought by buyers in C++
- Maximum bishops that can be placed on N*N chessboard in C++
- Maximum Number of Events That Can Be Attended in C++
- Maximum number of people that can be killed with strength P in C++
- Maximum elements that can be made equal with k updates in C++
- Maximum number that can be display on Seven Segment Display using N segments in C++
- Maximum money that can be withdrawn in two steps in C
- Maximum number of threads that can be created within a process in C
- Set the maximum width that a box can be with CSS
- Set the maximum height that a box can be with CSS
- Maximum number of partitions that can be sorted individually to make sorted in C++
- Maximum possible time that can be formed from four digits in C++
- Maximum given sized rectangles that can be cut out of a sheet of paper in C
- What can be done to control water pollution?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given N rupees. The goal is to buy maximum water possible with the money where the rates of water bottles are as follows −

- Plastic Bottles: A Rupees for 1 litre
- Glass Bottles: B Rupees for 1 litre
- Glass Bottles: B Rupees for 1 litre

Now the original cost of glass bottles becomes B-E rupees. After returning.

If the cost of plastic bottles is still less than B-E, then only buy plastic bottles. Else buy N-E/B-E glass bottles and spend rest on plastic bottles.

N = 6, A = 5, B = 4, E = 3;

Maximum litres of water: 3

**Explanation** − B-E=1, 1<A N-E=3, for 3 rupees we bought 3 1 litre glass bottles

N = 10, A = 5, B = 10, E = 3;

Maximum litres of water: 2

**Explanation** − B-E=7, 7>A n/a= 10/5 = 2 plastic bottles can be purchased

Integers money, bottle, gbottle, and gempty are used for rates and amount of money we have.

Function maxWater(int mny,int pb,int gb,int ge) takes all values as parameters and prints the amount of water that can be purchased.

Variable litrs is used to store the calculated amount of water in litres.

We pass pb as a new value of glass bottle(original value-return value).

If passed gb value is less than pb value, then purchase (mny-ge)/gb glass bottles.

Subtract this amount to calculate left-over money, mny-=ltrs*gb

Now plastic bottles that can be purchased is mny/pb.

If the passed pb value is more than pb value then only purchase mny/pb plastic bottles.

Print results in both cases as no. of bottles is no. of litres of water. Each bottle has 1 litre capacity.

// CPP implementation of the above approach #include<bits/stdc++.h> using namespace std; void maxWater(int mny,int pb,int gb,int ge){ int litrs; // if buying glass bottles is profitable if (gb < pb){ // no. of glass bottles int tmp=mny-ge/gb; litrs=tmp>0?tmp:0; mny-=litrs*gb; // no. of plastic bottles litrs+=mny/pb; cout<<"Maximum Liters of water : "<<litrs<<endl; } // only plastic bottles else cout<<"Maximum Liters of water only Plastic bottles: "<<(mny /pb)<<endl; } int main(){ int money = 20, pbottle=5, gbottle=10, gempty = 8; gbottle=gbottle-gempty; //new cost of glass botlles maxWater( money,pbottle,gbottle,gempty ); }

Maximum Liters of water: 14

Advertisements