Similar Problems
Similar Problems not available
Put Marbles In Bags  Leetcode Solution
LeetCode: Put Marbles In Bags Leetcode Solution
Difficulty: Hard
Topics: greedy sorting heappriorityqueue array
The Put Marbles In Bags problem on Leetcode is a mediumlevel algorithm problem that requires you to divide a given number of marbles between the given number of bags in the most optimal manner. The problem statement demands you to maximize the minimum number of marbles in each bag.
Problem Statement: You have a bag with marbles. You want to divide the marbles among several bags in such a way that the following conditions are met:

You are given an array of n integers, where arr[i] represents the number of marbles that should be put in the ith bag.

There are m bags, numbered from 1 to m.

Each bag can hold arbitrarily many marbles, but the total number of marbles in all bags should be equal to the number of marbles in the original bag.

Find the maximum number of marbles you can put in any bag such that every bag has at least that many marbles.
Solution Approach:
This problem is similar to the classic binary search problem of finding the smallest element greater than the target element in a given array. By considering the number we can obtain a suitable lower limit and a suitable upper limit on each ith bags. We can then use binary search to find the optimal solution in the search space between these limits.
To find the lower limit, we start with the first bag and assume we put all the marbles in it. This would be to maximize the minimum number of marbles in the bag, thus set it as the lower limit. We then proceed to the remaining bags and divide the remaining marbles equally, so that each bag would have the same number of marbles as the first bag the lower limit we just found.
To find the upper limit, we add up all the marbles in the bags such that every bag has the same maximum value. We then divide this sum by the number of the bags. This result will give us the upper limit for all the bags since any bag that can hold more than this number will have more marbles than the maximum number of marbles in the bags.
Once we have found the upper and lower limits for all the bags, we can begin our binary search algorithm. We will iterate over the range between the lower and upper limits to find the values of the minimum number of marbles in each bag. We can then check if dividing the marbles in this way is a valid solution to the problem.
If the solution is valid, we can increase the lower limit to the mid value of the lower and upper limits, as this will guarantee that we have the maximum possible number of marbles in each bag. If the solution is invalid, we can decrease the upper limit to the mid value of the lower and upper limits.
We can repeat the binary search until we find the lowest solution.
Code Implementation:
Here is the sample code that implements the approach described above:
class Solution {
public:
int minimumSize(vector<int>& arr, int m) {
int n = arr.size();
long long s = 0;
for (int x: arr) s += x;
int l = 1, r = s;
while (l < r) {
int mid = (l + r) / 2;
int cnt = 0;
long long sum = 0;
for (int i = 0; i < n; ++i) {
if (arr[i] > mid) {
cnt = m + 1;
break;
}
if (sum + arr[i] > mid) {
++cnt;
sum = arr[i];
} else {
sum += arr[i];
}
}
if (cnt < m) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
};
Conclusion: The Put Marbles In Bags problem can be solved by using the binary search algorithm to find the maximum number of marbles that can be placed in any bag such that every bag has at least that many marbles. By implementing the approach described above, you can solve this problem and achieve the desired output.
Put Marbles In Bags Solution Code
1