|
| 1 | +<h2><a href="https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/">1567. Maximum Length of Subarray With Positive Product</a></h2><h3>Medium</h3><hr><div><p>Given an array of integers <code>nums</code>, find the maximum length of a subarray where the product of all its elements is positive.</p> |
| 2 | + |
| 3 | +<p>A subarray of an array is a consecutive sequence of zero or more values taken out of that array.</p> |
| 4 | + |
| 5 | +<p>Return <em>the maximum length of a subarray with positive product</em>.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong>Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre><strong>Input:</strong> nums = [1,-2,-3,4] |
| 11 | +<strong>Output:</strong> 4 |
| 12 | +<strong>Explanation:</strong> The array nums already has a positive product of 24. |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p><strong>Example 2:</strong></p> |
| 16 | + |
| 17 | +<pre><strong>Input:</strong> nums = [0,1,-2,-3,-4] |
| 18 | +<strong>Output:</strong> 3 |
| 19 | +<strong>Explanation:</strong> The longest subarray with positive product is [1,-2,-3] which has a product of 6. |
| 20 | +Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive.</pre> |
| 21 | + |
| 22 | +<p><strong>Example 3:</strong></p> |
| 23 | + |
| 24 | +<pre><strong>Input:</strong> nums = [-1,-2,-3,0,1] |
| 25 | +<strong>Output:</strong> 2 |
| 26 | +<strong>Explanation:</strong> The longest subarray with positive product is [-1,-2] or [-2,-3]. |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | +<p><strong>Constraints:</strong></p> |
| 31 | + |
| 32 | +<ul> |
| 33 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 34 | + <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> |
| 35 | +</ul> |
| 36 | +</div> |
0 commit comments