Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
The min-product of an array is equal to the minimum value in the array multiplied by the array's sum.
[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.
Example 1:
Input: nums = [1,2,3,2] Output: 14 Explanation: The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2). 2 * (2+3+2) = 2 * 7 = 14.
Example 2:
Input: nums = [2,3,3,1,2] Output: 18 Explanation: The maximum min-product is achieved with the subarray [3,3] (minimum value is 3). 3 * (3+3) = 3 * 6 = 18.
Example 3:
Input: nums = [3,1,5,6,4,2] Output: 60 Explanation: The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4). 4 * (5+6+4) = 4 * 15 = 60.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 107Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Stack
[1,2,3,2]
[2,3,3,1,2]
[3,1,5,6,4,2]
subarray-with-elements-greater-than-varying-threshold)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1856: Maximum Subarray Min-Product
class Solution {
public int maxSumMinProduct(int[] nums) {
int n = nums.length;
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(left, -1);
Arrays.fill(right, n);
Deque<Integer> stk = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
while (!stk.isEmpty() && nums[stk.peek()] >= nums[i]) {
stk.pop();
}
if (!stk.isEmpty()) {
left[i] = stk.peek();
}
stk.push(i);
}
stk.clear();
for (int i = n - 1; i >= 0; --i) {
while (!stk.isEmpty() && nums[stk.peek()] > nums[i]) {
stk.pop();
}
if (!stk.isEmpty()) {
right[i] = stk.peek();
}
stk.push(i);
}
long[] s = new long[n + 1];
for (int i = 0; i < n; ++i) {
s[i + 1] = s[i] + nums[i];
}
long ans = 0;
for (int i = 0; i < n; ++i) {
ans = Math.max(ans, nums[i] * (s[right[i]] - s[left[i] + 1]));
}
final int mod = (int) 1e9 + 7;
return (int) (ans % mod);
}
}
// Accepted solution for LeetCode #1856: Maximum Subarray Min-Product
func maxSumMinProduct(nums []int) int {
n := len(nums)
left := make([]int, n)
right := make([]int, n)
for i := range left {
left[i] = -1
right[i] = n
}
stk := []int{}
for i, x := range nums {
for len(stk) > 0 && nums[stk[len(stk)-1]] >= x {
stk = stk[:len(stk)-1]
}
if len(stk) > 0 {
left[i] = stk[len(stk)-1]
}
stk = append(stk, i)
}
stk = []int{}
for i := n - 1; i >= 0; i-- {
for len(stk) > 0 && nums[stk[len(stk)-1]] > nums[i] {
stk = stk[:len(stk)-1]
}
if len(stk) > 0 {
right[i] = stk[len(stk)-1]
}
stk = append(stk, i)
}
s := make([]int, n+1)
for i, x := range nums {
s[i+1] = s[i] + x
}
ans := 0
for i, x := range nums {
if t := x * (s[right[i]] - s[left[i]+1]); ans < t {
ans = t
}
}
const mod = 1e9 + 7
return ans % mod
}
# Accepted solution for LeetCode #1856: Maximum Subarray Min-Product
class Solution:
def maxSumMinProduct(self, nums: List[int]) -> int:
n = len(nums)
left = [-1] * n
right = [n] * n
stk = []
for i, x in enumerate(nums):
while stk and nums[stk[-1]] >= x:
stk.pop()
if stk:
left[i] = stk[-1]
stk.append(i)
stk = []
for i in range(n - 1, -1, -1):
while stk and nums[stk[-1]] > nums[i]:
stk.pop()
if stk:
right[i] = stk[-1]
stk.append(i)
s = list(accumulate(nums, initial=0))
mod = 10**9 + 7
return max((s[right[i]] - s[left[i] + 1]) * x for i, x in enumerate(nums)) % mod
// Accepted solution for LeetCode #1856: Maximum Subarray Min-Product
/**
* [1856] Maximum Subarray Min-Product
*
* 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 10^9 + 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.
*
* Example 1:
*
* Input: nums = [1,<u>2,3,2</u>]
* Output: 14
* Explanation: The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2).
* 2 * (2+3+2) = 2 * 7 = 14.
*
* Example 2:
*
* Input: nums = [2,<u>3,3</u>,1,2]
* Output: 18
* Explanation: The maximum min-product is achieved with the subarray [3,3] (minimum value is 3).
* 3 * (3+3) = 3 * 6 = 18.
*
* Example 3:
*
* Input: nums = [3,1,<u>5,6,4</u>,2]
* Output: 60
* Explanation: The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4).
* 4 * (5+6+4) = 4 * 15 = 60.
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* 1 <= nums[i] <= 10^7
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-subarray-min-product/
// discuss: https://leetcode.com/problems/maximum-subarray-min-product/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn max_sum_min_product(nums: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1856_example_1() {
let nums = vec![1, 2, 3, 2];
let result = 14;
assert_eq!(Solution::max_sum_min_product(nums), result);
}
#[test]
#[ignore]
fn test_1856_example_2() {
let nums = vec![2, 3, 3, 1, 2];
let result = 18;
assert_eq!(Solution::max_sum_min_product(nums), result);
}
#[test]
#[ignore]
fn test_1856_example_3() {
let nums = vec![3, 1, 5, 6, 4, 2];
let result = 60;
assert_eq!(Solution::max_sum_min_product(nums), result);
}
}
// Accepted solution for LeetCode #1856: Maximum Subarray Min-Product
function maxSumMinProduct(nums: number[]): number {
const n = nums.length;
const left: number[] = Array(n).fill(-1);
const right: number[] = Array(n).fill(n);
const stk: number[] = [];
for (let i = 0; i < n; ++i) {
while (stk.length && nums[stk.at(-1)!] >= nums[i]) {
stk.pop();
}
if (stk.length) {
left[i] = stk.at(-1)!;
}
stk.push(i);
}
stk.length = 0;
for (let i = n - 1; i >= 0; --i) {
while (stk.length && nums[stk.at(-1)!] > nums[i]) {
stk.pop();
}
if (stk.length) {
right[i] = stk.at(-1)!;
}
stk.push(i);
}
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] + nums[i];
}
let ans: bigint = 0n;
const mod = 10 ** 9 + 7;
for (let i = 0; i < n; ++i) {
const t = BigInt(nums[i]) * BigInt(s[right[i]] - s[left[i] + 1]);
if (ans < t) {
ans = t;
}
}
return Number(ans % BigInt(mod));
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan left (or right) to find the next greater/smaller element. The inner scan can visit up to n elements per outer iteration, giving O(n²) total comparisons. No extra space needed beyond loop variables.
Each element is pushed onto the stack at most once and popped at most once, giving 2n total operations = O(n). The stack itself holds at most n elements in the worst case. The key insight: amortized O(1) per element despite the inner while-loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.