Magicsheet logo

Maximum Subarray Min-Product

Medium
12.5%
Updated 8/1/2025

Maximum Subarray Min-Product

1. What is this problem about?

The Maximum Subarray Min-Product problem introduces a unique scoring metric for subarrays. The "min-product" of a subarray is defined as the minimum value in that subarray multiplied by the sum of all elements in that same subarray. Given an array of positive integers, you need to find the maximum possible min-product among all possible contiguous subarrays. Because the results can be very large, you are often asked to return the answer modulo 10^9 + 7, though the intermediate calculations must be precise.

2. Why is this asked in interviews?

This "Maximum Subarray Min-Product interview question" is a favorite for companies like Uber and Amazon because it requires combining two different techniques: prefix sums for quick range additions and monotonic stacks for finding boundaries. It tests your ability to efficiently identify the range over which a specific element is the minimum. It's a great way to see if a candidate can optimize a problem that looks like it might require O(N^2) into a much faster O(N) solution.

3. Algorithmic pattern used

The "Array, Monotonic Stack, Stack, Prefix Sum interview pattern" is the key to solving this. For each element in the array, we want to treat it as the "minimum" and find the largest possible subarray where this is true. This means finding the first element to the left and the first element to the right that are smaller than it. A monotonic stack helps us find these boundaries for all elements in linear time. We then use a prefix sum array to calculate the total sum of the elements within those boundaries instantly.

4. Example explanation

Consider the array [1, 2, 3, 2].

  • For the element '3', it is the minimum only in the subarray [3]. Min-product = 3 * 3 = 9.
  • For the first '2', it is the minimum in the subarray [2, 3, 2]. Sum = 2+3+2 = 7. Min-product = 2 * 7 = 14.
  • For the second '2', it is also the minimum in [2, 3, 2]. Same result: 14.
  • For the '1', it is the minimum for the entire array [1, 2, 3, 2]. Sum = 8. Min-product = 1 * 8 = 8. The maximum min-product is 14.

5. Common mistakes candidates make

One frequent mistake in the "Maximum Subarray Min-Product coding problem" is not using 64-bit integers (like long in Java or C++) for the intermediate sum and product calculations. Even if the final answer is returned modulo 10^9 + 7, the product before the modulo can easily exceed the range of a standard 32-bit integer. Another mistake is incorrectly handling the boundaries in the monotonic stack, leading to "off-by-one" errors that include or exclude elements incorrectly.

6. Interview preparation tip

Practice using monotonic stacks to find the "nearest smaller" or "nearest larger" elements. This is a recurring theme in many hard array problems. Also, get comfortable with Prefix Sums as they are the fastest way to handle range sum queries. When practicing this problem, try to explain clearly why treating each element as a potential minimum and expanding its range is an exhaustive way to find the global maximum—this clarity is what interviewers look for.

Similar Questions