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. Show ExampleIf N = 5, K = 4, capacity[] = {1, 2, 3, 2, 1}.
Algorithm
Example#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; }OutputWhen you compile and execute above program. It generates following output − Min bottles required = 1
Code
|