← DiffPush

Maximum product subarray

DiffPush Tier Arrays · Hard O(n) · O(1)

Best Compounding Run in a Yield Series

A portfolio system stores a multiplier per period, where a negative multiplier means the position was inverted. The risk desk wants the contiguous run of periods whose multipliers compound to the largest product. A run must be non-empty, and zeros reset the compounding entirely.

Input: An array nums of n integers, one multiplier per period.

Output: The largest product obtainable from a non-empty contiguous run of nums.

Constraints

Examples

Example 1

Input: {"nums":[2,3,-2,4]}
Output: 6
The opening pair compounds to 6, which beats any run that includes the negative multiplier.

Example 2

Input: {"nums":[-2,0,-1]}
Output: 0
The zero splits the series, and a single negative multiplier yields less than zero, so nothing beats 0.

Solve this in your browser →

Also on LeetCode ↗