Maximum Subarray Min-Product - Problem

The min-product of an array is equal to the minimum value in the array multiplied by the array's sum.

For example, the array [3,2,5] (minimum value is 2) has a min-product of 2 * (3+2+5) = 2 * 10 = 20.

Given an array of integers nums, return the maximum min-product of any non-empty subarray of nums. Since the answer may be large, return it modulo 109 + 7.

Note that the min-product should be maximized before performing the modulo operation. Testcases are generated such that the maximum min-product without modulo will fit in a 64-bit signed integer.

A subarray is a contiguous part of an array.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,2]
Output: 14
💡 Note: The subarray [2,3,2] has minimum 2 and sum 7, giving min-product = 2 × 7 = 14, which is maximum among all subarrays.
Example 2 — Single Element
$ Input: nums = [2,3,3,1,2]
Output: 18
💡 Note: The subarray [2,3,3] has minimum 2 and sum 8, giving min-product = 2 × 8 = 16. But [3,3] has min-product = 3 × 6 = 18.
Example 3 — Large Numbers
$ Input: nums = [3,1,5,6,4,2]
Output: 60
💡 Note: The subarray [5,6,4] has minimum 4 and sum 15, giving min-product = 4 × 15 = 60, which is the maximum.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 107

Visualization

Tap to expand
Maximum Subarray Min-Product ProblemInput Array:1232Example Subarrays and Min-Products:[1]: min=1, sum=1 → product=1[2]: min=2, sum=2 → product=4[2,3]: min=2, sum=5 → product=10[2,3,2]: min=2, sum=7 → product=14 ✓[3]: min=3, sum=3 → product=9[1,2,3]: min=1, sum=6 → product=6Optimal subarrayMaximum Min-Product: 14
Understanding the Visualization
1
Input Array
Given array [1,2,3,2] with different elements
2
Find Subarrays
Consider all possible contiguous subarrays
3
Calculate Min-Products
For each subarray: min-product = minimum × sum
Key Takeaway
🎯 Key Insight: For each element as the minimum, find the largest possible subarray containing it using monotonic stack
Asked in
Google 25 Amazon 18 Microsoft 15 Apple 12
28.4K Views
Medium Frequency
~25 min Avg. Time
982 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