Maximum Subarray Min-Product - Problem
Maximum Subarray Min-Product

You're given an array of integers and need to find the maximum min-product among all possible non-empty subarrays.

The min-product of a subarray is calculated as:
minimum_value × sum_of_all_elements

For example, in subarray [3, 2, 5]:
• Minimum value: 2
• Sum: 3 + 2 + 5 = 10
• Min-product: 2 × 10 = 20

Your goal is to find the subarray that gives the highest min-product value. Since the result can be very large, return it modulo 109 + 7.

Key insight: You want to find the optimal balance between having a large minimum value and a large sum in your subarray.

Input & Output

example_1.py — Basic case
$ Input: [1,2,3,2]
Output: 14
💡 Note: The maximum min-product is achieved by subarray [2,3,2] with minimum=2 and sum=7, giving min-product=2×7=14
example_2.py — Single element optimal
$ Input: [2,3,3,1,2]
Output: 18
💡 Note: The maximum min-product is achieved by subarray [3,3] with minimum=3 and sum=6, giving min-product=3×6=18
example_3.py — Entire array optimal
$ Input: [3,1,5,6,4,2]
Output: 60
💡 Note: The maximum min-product is achieved by subarray [5,6] with minimum=5 and sum=11, giving min-product=5×11=55. Actually, it's [5,6,4] with min=4, sum=15, product=60

Visualization

Tap to expand
🏗️ Construction Project OptimizationBuilding Materials (Array Elements):1Weak2Medium3Strong2MediumProject Analysis:Section [2,3,2]: Weakest=2, Total=7, Profit=2×7=14Section [3]: Weakest=3, Total=3, Profit=3×3=9Section [1,2,3,2]: Weakest=1, Total=8, Profit=1×8=8🎯 Optimal Strategy1. For each material strength level2. Find longest section where it's weakest3. Calculate: strength × section_value4. Choose maximum profitMaximum Profit: 14 (Section [2,3,2])
Understanding the Visualization
1
Identify Materials
Each array element represents a building material with a certain strength value
2
Find Optimal Sections
For each material, find the longest section where it's the weakest (determines project strength)
3
Calculate Profits
Profit = material_strength × sum_of_all_materials_in_section
4
Choose Maximum
Select the section that gives the highest profit
Key Takeaway
🎯 Key Insight: Each element can be the minimum in exactly one optimal subarray. Use monotonic stack to efficiently find the boundaries where each element is the bottleneck, maximizing the balance between minimum value and subarray sum.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Each element is pushed and popped from stack at most once, prefix sum is O(n)

n
2n
Linear Growth
Space Complexity
O(n)

Space for prefix sum array and monotonic stack

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 106
  • Return answer modulo 109 + 7
  • All array elements are positive integers
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.0K Views
Medium-High Frequency
~25 min Avg. Time
1.5K 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