- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find maximum length of subarray with positive product in Python

Suppose we have an array called nums, we have to find the maximum length of a subarray where the product of all its elements is positive. We have to find the maximum length of a subarray with positive product.

So, if the input is like nums = [2,-2,-4,5,-3], then the output will be 4 because first four elements are forming a subarray whose product is positive.

To solve this, we will follow these steps :

- Define a function util() . This will take s, e
- neg := 0
- ns := -1, ne := -1
- for i in range s to e, do
- if nums[i] < 0, then
- neg := neg + 1
- if ns is same as -1, then
- ns := i

- ne := i

- if nums[i] < 0, then
- if neg is even, then
- return e-s+1

- otherwise,
- return maximum of e-ns and ne-s

- From the main method, do the following −
- ans := 0
- s := -1, e := -1
- for i in range 0 to size of nums, do
- if nums[i] is not same as 0 and s is same as -1, then
- s := i

- otherwise when nums[i] is same as 0 and s is not same as -1, then
- e := i-1
- ans := maximum of ans and util(s, e)
- s := -1, e := -1

- if nums[i] is not same as 0 and s is same as -1, then
- if s is not same as -1 and e is same as -1, then
- e := size of nums -1
- ans := maximum of ans and util(s, e)

- return ans

Let us see the following implementation to get better understanding:

## Example

def util(s, e): neg = 0 ns, ne = -1, -1 for i in range(s, e+1): if nums[i]<0: neg += 1 if ns == -1: ns = i ne = i if neg == 0 or neg %2 == 0: return e-s+1 else: return max(e-ns, ne-s) def solve(nums): ans = 0 s, e = -1, -1 for i in range(len(nums)): if nums[i]!=0 and s == -1: s = i elif nums[i]==0 and s != -1: e = i-1 ans = max(ans, util(s, e)) s = -1 e = -1 if s!= -1 and e == -1: e = len(nums)-1 ans = max(ans, util(s, e)) return ans nums = [2,-2,-4,5,-3] print(solve(nums))

## Input

[2,-2,-4,5,-3]

## Output

4

- Related Questions & Answers
- Program to find maximum product of contiguous subarray in Python
- Program to find maximum subarray min-product in Python
- Maximum length subarray with LCM equal to product in C++
- Maximum Product Subarray in Python
- Find maximum average subarray of k length in C++
- Program to find maximum absolute sum of any subarray in Python
- Program to find maximum score of a good subarray in Python
- Program to find maximum length of k ribbons of same length in Python
- Program to find maximum ascending subarray sum using Python
- Maximum Product Subarray | Added negative product case in C++
- Finding maximum length of common subarray in JavaScript
- Program to find out the greatest subarray of a given length in python
- Program to find dot product of run length encoded vectors in Python
- Program to find maximum length of non-sharing words in Python
- Program to find length of shortest sublist with maximum frequent element with same frequency in Python

Advertisements