1567. Maximum Length of Subarray With Positive Product Medium
1/**
2 * [1567] Maximum Length of Subarray With Positive Product
3 *
4 * Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive.
5 * A subarray of an array is a consecutive sequence of zero or more values taken out of that array.
6 * Return the maximum length of a subarray with positive product.
7 *
8 * Example 1:
9 *
10 * Input: nums = [1,-2,-3,4]
11 * Output: 4
12 * Explanation: The array nums already has a positive product of 24.
13 *
14 * Example 2:
15 *
16 * Input: nums = [0,1,-2,-3,-4]
17 * Output: 3
18 * Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6.
19 * Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive.
20 * Example 3:
21 *
22 * Input: nums = [-1,-2,-3,0,1]
23 * Output: 2
24 * Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3].
25 *
26 *
27 * Constraints:
28 *
29 * 1 <= nums.length <= 10^5
30 * -10^9 <= nums[i] <= 10^9
31 *
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/
36// discuss: https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41 pub fn get_max_len(nums: Vec<i32>) -> i32 {
42 0
43 }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn test_1567() {
54 }
55}
56
Back
© 2025 bowen.ge All Rights Reserved.