- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

Given N glasses having water, and a list of each of their capacity. The task is to find the minimum number of bottles required to fill out exactly K glasses. The capacity of each bottle is 100 units.

If N = 5, K = 4, capacity[] = {1, 2, 3, 2, 1}.

- Filling the glasses with capacities 2, 3, 2, requires 8units.
- This way, it's enough to open just 1 bottle.

- To fill out exactly K glasses, take the K glasses with least capacity
Total required bottles can be calculated as −

Ceil value of (Sum of capacities of 1st k glasses) / (Capacity of 1 bottle).

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int minBottles(int *capacity, int n, int k) { sort(capacity, capacity + n); int sum = 0; for (int i = 0; i < k; ++i) { sum += capacity[i]; } return ceil((double)sum/100); } int main() { int capacity[] = {1, 2, 3, 2, 1}; cout << "Min bottles required = " <<minBottles(capacity, 5, 4) << endl; return 0; }

When you compile and execute above program. It generates following output −

Min bottles required = 1

- Related Questions & Answers
- Minimum flips required to maximize a number with k set bits in C++.
- Minimum number of swaps required to sort an array in C++
- Program to find minimum number of bricks required to make k towers of same height in Python
- Minimum Number of K Consecutive Bit Flips in C++
- Minimum number of mails required to distribute all the questions using C++.
- Minimum Number of Platforms Required for a Railway Station using C++.
- Minimum number of operations required to sum to binary string S using C++.
- Program to find minimum number of pins required to hang all banners in C++
- Program to find minimum number of steps required to catch the opponent in C++
- Minimum number of operations required to delete all elements of the array using C++.
- Minimum number of given operations required to make two strings equal using C++.
- Minimum number of palindromes required to express N as a sum using C++.
- Find out the minimum number of coins required to pay total amount in C++
- Beer Bottles in C++
- Minimum number of given moves required to make N divisible by 25 using C++.

Advertisements