Ctrl K

Longest Run With a Positive Product

Medium
You get an integer array values. Find the longest contiguous span whose elements multiply to a strictly positive number, and return its length. If there is no such span, return 0. You do not need the product itself, which may be huge; only its sign matters.
Example 1
values=[3, -1, 0, -2, -5, 4, -1] → 3

After the zero comes [-2, -5, 4]: two negatives cancel out, the product is 40 > 0, length 3. Adding the final -1 would make three negatives.

Example 2
values=[-7] → 0

The only element is negative, so no positive product is possible and the answer is 0.

Example 3
values=[2, -3, 1, 6, 0, 5] → 2
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumReach the Last Cell MediumAs Many Trades as You Like MediumLongest Seesaw Subsequence MediumFewest Meetings to Cancel Medium
loading Python…
Input
values=[3, -1, 0, -2, -5, 4, -1]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 125 tests on the server, hidden ones included.