# Daily Coding Challenge #39

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.

``````/*
LeetCode - Minimum Number of Days to Make m Bouquets

Given an integer array bloomDay, an integer m and an integer k.

We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.

The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.

Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.

Example 1:

Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _]   // we can only make one bouquet.
After day 2: [x, _, _, _, x]   // we can only make two bouquets.
After day 3: [x, _, x, _, x]   // we can make 3 bouquets. The answer is 3.
Example 2:

Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:

Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:

Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:

Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9

Constraints:

bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
*/

class Solution {
public:
bool helper(vector<int>& bloomDay, int m, int k, int mid){
int kk=0,mm=0;
for(int i=0;i<bloomDay.size();i++){
if(bloomDay[i]<=mid) {
// we can make a bouquet
kk++;
} else {
// set it to 0 if not
kk=0;
}

if(kk==k) {
// we can use k adjacent flowers
kk=0;
mm++;
}
// we can make m bouquets
if(mm==m) return true;
}

return false;
}

int minDays(vector<int>& bloomDay, int m, int k) {
int n = (int) bloomDay.size();
if(n==0||m==0||k==0) return 0;
if(m*k>n) return -1;
int l=*min_element(bloomDay.begin(),bloomDay.end());
int r=*max_element(bloomDay.begin(),bloomDay.end());
// binary search approach
while(l<=r){
int mid=l+(r-l)/2;
if(helper(bloomDay,m,k,mid)){
r=mid-1;
} else {
l=mid+1;
}
}
return l;
}
};
``````

``````/*
LeetCode - Sum of Mutated Array Closest to Target

Given an integer array arr and a target value target, return the integer value such that when we change all the integers larger than value in the given array to be equal to value, the sum of the array gets as close as possible (in absolute difference) to target.

In case of a tie, return the minimum such integer.

Notice that the answer is not neccesarilly a number from arr.

Example 1:

Input: arr = [4,9,3], target = 10
Output: 3
Explanation: When using 3 arr converts to [3, 3, 3] which sums 9 and that's the optimal answer.
Example 2:

Input: arr = [2,3,5], target = 10
Output: 5
Example 3:

Input: arr = [60864,25176,27249,21296,20204], target = 56803
Output: 11361

Constraints:

1 <= arr.length <= 10^4
1 <= arr[i], target <= 10^5
*/

class Solution {
public:
int helper(vector<int>& arr, int target, int mid){
int sum=0;
for(int a:arr) sum+=min(a,mid);
return abs(sum-target);
}

int findBestValue(vector<int>& arr, int target) {
// binary search approach
int n=(int)arr.size();
if(n==0) return 0;
sort(arr.begin(),arr.end());
int l=arr;
int r=arr[n-1];
while(l<r){
int mid=l+(r-l)/2;
int d1=helper(arr,target,mid);
int d2=helper(arr,target,mid-1);
if(d1>d2){
r=mid-1;
} else {
l=mid+1;
}
}
return l;
}
};
``````

``````/*
LeetCode - Find the Smallest Divisor Given a Threshold

Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold.

Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5).

It is guaranteed that there will be an answer.

Example 1:

Input: nums = [1,2,5,9], threshold = 6
Output: 5
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1.
If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2).
Example 2:

Input: nums = [2,3,5,7,11], threshold = 11
Output: 3
Example 3:

Input: nums = , threshold = 5
Output: 4

Constraints:

1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 10^6
nums.length <= threshold <= 10^6
*/

class Solution {
public:
int helper(vector<int>& nums, int mid, int threshold){
int sum=0;
for(int n:nums) sum+=(n/mid)+(n%mid?1:0);
return sum;
}

int smallestDivisor(vector<int>& nums, int threshold) {
// binary search approach
int l=1,r=1e6;
while(l<r){
int mid=l+(r-l)/2;
int sum = helper(nums,mid,threshold);
if(sum>threshold) l=mid+1;
else r=mid;
}
return l;
}
};
``````

The source code is available in corresponding repo below. Star and watch for timely updates!

## wingkwong / atcoder

### Discussion   