Maximum Subarray Min Product

Medium Topics Company Tags

For a nonempty contiguous subarray of nums, multiply its smallest value by the sum of all its values. Find the largest product obtainable this way, then return that product modulo 1000000007.

Compare the full products before applying the modulo. The maximum product before modulo is guaranteed to fit in a signed 64-bit integer.

Example 1:

Input: nums = [2,4,2]

Output: 16

Explanation: The whole array has minimum 2 and sum 8, giving a product of 16. The singleton [4] also gives 16, and no other subarray gives more.

Example 2:

Input: nums = [5]

Output: 25

Explanation: The only nonempty subarray is [5], with minimum and sum both equal to 5. Its product is 25.

Example 3:

Input: nums = [3,3,3]

Output: 27

Explanation: The whole array has minimum 3 and sum 9, giving 27. Every shorter subarray has the same minimum but a smaller sum.

Constraints:

  • 1 <= nums.length <= 100000
  • 1 <= nums[i] <= 10000000
  • The maximum product before modulo fits in a signed 64-bit integer.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.