#include <bits/stdc++.h>
using namespace std;
int findMax(vector<int> &v) {
int maxi = INT_MIN;
int n = v.size();
//find the maximum:
for (int i = 0; i < n; i++) {
maxi = max(maxi, v[i]);
}
return maxi;
}
int calculateTotalHours(vector<int> &v, int hourly) {
int totalH = 0;
int n = v.size();
//find total hours:
for (int i = 0; i < n; i++) {
totalH += ceil((double)(v[i]) / (double)(hourly));
}
return totalH;
}
int minimumRateToEatBananas(vector<int> v, int h) {
int low = 1, high = findMax(v);
//apply binary search:
while (low <= high) {
int mid = (low + high) / 2;
int totalH = calculateTotalHours(v, mid);
if (totalH <= h) {
high = mid - 1;
}
else {
low = mid + 1;
}
}
return low;
}
int main()
{
vector<int> v = {7, 15, 6, 3};
int h = 8;
int ans = minimumRateToEatBananas(v, h);
cout << "Koko should eat atleast " << ans << " bananas/hr.\n";
return 0;
}
CgoKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgZmluZE1heCh2ZWN0b3I8aW50PiAmdikgewogICAgaW50IG1heGkgPSBJTlRfTUlOOwogICAgaW50IG4gPSB2LnNpemUoKTsKICAgIC8vZmluZCB0aGUgbWF4aW11bToKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgbWF4aSA9IG1heChtYXhpLCB2W2ldKTsKICAgIH0KICAgIHJldHVybiBtYXhpOwp9CgppbnQgY2FsY3VsYXRlVG90YWxIb3Vycyh2ZWN0b3I8aW50PiAmdiwgaW50IGhvdXJseSkgewogICAgaW50IHRvdGFsSCA9IDA7CiAgICBpbnQgbiA9IHYuc2l6ZSgpOwogICAgLy9maW5kIHRvdGFsIGhvdXJzOgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICB0b3RhbEggKz0gY2VpbCgoZG91YmxlKSh2W2ldKSAvIChkb3VibGUpKGhvdXJseSkpOwogICAgfQogICAgcmV0dXJuIHRvdGFsSDsKfQo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