Minimized Maximum of Products Distributed to Any Store - Problem

You are given an integer n indicating there are n specialty retail stores. There are m product types of varying amounts, which are given as a 0-indexed integer array quantities, where quantities[i] represents the number of products of the i-th product type.

You need to distribute all products to the retail stores following these rules:

  • A store can only be given at most one product type but can be given any amount of it.
  • After distribution, each store will have been given some number of products (possibly 0). Let x represent the maximum number of products given to any store. You want x to be as small as possible, i.e., you want to minimize the maximum number of products that are given to any store.

Return the minimum possible x.

Input & Output

Example 1 — Basic Case
$ Input: n = 6, quantities = [11,6]
Output: 3
💡 Note: With max 3 per store: Product type 0 needs ceil(11/3) = 4 stores, product type 1 needs ceil(6/3) = 2 stores. Total: 4 + 2 = 6 stores ≤ 6.
Example 2 — Single Product Type
$ Input: n = 7, quantities = [15]
Output: 3
💡 Note: Only one product type with 15 units. With max 3 per store: ceil(15/3) = 5 stores ≤ 7. With max 2: ceil(15/2) = 8 stores > 7. So minimum possible maximum is 3.
Example 3 — Multiple Small Quantities
$ Input: n = 1, quantities = [100000]
Output: 100000
💡 Note: Only 1 store available, so it must take all 100000 products of the single type.

Constraints

  • m == quantities.length
  • 1 ≤ m ≤ n ≤ 105
  • 1 ≤ quantities[i] ≤ 105

Visualization

Tap to expand
Minimized Maximum Products Distribution INPUT n = 6 Retail Stores S1 S2 S3 S4 S5 S6 quantities array: 11 Product A 6 Product B Input Values n = 6 quantities = [11, 6] ALGORITHM STEPS 1 Binary Search Setup lo=1, hi=max(quantities)=11 2 Check mid value Can we distribute with max x products per store? 3 Count stores needed For each product type: ceil(qty[i] / x) 4 Adjust search range If stores needed <= n: hi = mid (try smaller) Else: lo = mid + 1 Binary Search Progress: mid=6: need 2+1=3 OK mid=3: need 4+2=6 OK mid=2: need 6+3=9 FAIL FINAL RESULT Optimal Distribution (x = 3) Product A (11 items): 3 3 3 2 4 stores: 3+3+3+2 = 11 Product B (6 items): 3 3 2 stores: 3+3 = 6 Total: 4+2 = 6 stores (OK) OUTPUT 3 Key Insight: Binary search on the answer! Instead of trying all distributions, we search for the minimum value x where ceil(quantities[i]/x) summed for all products is <= n stores. Time: O(m * log(max(q))) TutorialsPoint - Minimized Maximum of Products Distributed to Any Store | Binary Search Approach
Asked in
Google 15 Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen