Given an array **arr[]** of **N** integers, the task is to find the length of the longest subarray whose product is greater than or equals to 0.

**Examples:**

Input:arr[] = {-1, 1, 1, -2, 3, 2, -1 }

Output:6

Explanation:

The longest subarray with product ≥ 0 = {1, 1, -2, 3, 2, -1} and {-1, 1, 1, -2, 3, 2}.

Length of each = 6.

Input:arr[] = {-1, -2, -3, -4}

Output:4

Explanation:

The longest subarray with product ≥ 0 = {-1, -2, -3, -4}.

Length = 4.

**Approach:**

- Check whether the product of all the elements in the given array is greater than or equals zero or not.
- If Yes then, the length of the longest subarray with a product greater than or equals to zero is the
**length of the array**. - If the above statement is not true, then the array contains an odd number of negative elements. In this case, to find the longest subarray do the following:
- For each negative element occurs in the array, the subarray to left and right of the current element gives the product which is greater than or equals to 0. Therefore the length of required longest subarray will be:
L = max(L, max(i, N - i - 1))

- Keep updating the length of the subarray for each negative element found in the array.
- The value of
**L**is the length of longest subarray with product greater than equals to 0.

- For each negative element occurs in the array, the subarray to left and right of the current element gives the product which is greater than or equals to 0. Therefore the length of required longest subarray will be:

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that count the length ` `// of longest subarray with product ` `// greater than or equals to zero ` `int` `maxLength(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `int` `product = 1, len = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `product *= arr[i]; ` ` ` `} ` ` ` ` ` `// If product is greater than ` ` ` `// zero, return array size ` ` ` `if` `(product >= 0) { ` ` ` `return` `N; ` ` ` `} ` ` ` ` ` `// Traverse the array and if ` ` ` `// any negative element found ` ` ` `// then update the length of ` ` ` `// longest subarray with the ` ` ` `// length of left and right subarray ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `if` `(arr[i] < 0) { ` ` ` `len = max(len, ` ` ` `max(N - i - 1, i)); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `len; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { -1, 1, 1, -2, 3, 2, -1 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << maxLength(arr, N) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` ` ` `public` `class` `GFG{ ` `// Function that count the length ` `// of longest subarray with product ` `// greater than or equals to zero ` ` ` `static` `int` `maxLength(` `int` `arr[], ` `int` `N) ` ` ` `{ ` ` ` `int` `product = ` `1` `, len = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` `product *= arr[i]; ` ` ` `} ` ` ` ` ` `// If product is greater than ` ` ` `// zero, return array size ` ` ` `if` `(product >= ` `0` `) { ` ` ` `return` `N; ` ` ` `} ` ` ` ` ` `// Traverse the array and if ` ` ` `// any negative element found ` ` ` `// then update the length of ` ` ` `// longest subarray with the ` ` ` `// length of left and right subarray ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) { ` ` ` `if` `(arr[i] < ` `0` `) { ` ` ` `len = Math.max(len, Math.max(N - i - ` `1` `, i)); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `len; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `arr[] = { -` `1` `, ` `1` `, ` `1` `, -` `2` `, ` `3` `, ` `2` `, -` `1` `}; ` ` ` `int` `N = arr.length; ` ` ` `System.out.println(maxLength(arr, N)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by AbhiThakur ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function that count the Length ` `# of longest subarray with product ` `# greater than or equals to zero ` `def` `maxLength(arr, N): ` ` ` `product ` `=` `1` ` ` `Len` `=` `0` ` ` ` ` `for` `i ` `in` `arr: ` ` ` `product ` `*` `=` `i ` ` ` ` ` `# If product is greater than ` ` ` `# zero, return array size ` ` ` `if` `(product >` `=` `0` `): ` ` ` `return` `N ` ` ` ` ` `# Traverse the array and if ` ` ` `# any negative element found ` ` ` `# then update the Length of ` ` ` `# longest subarray with the ` ` ` `# Length of left and right subarray ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `if` `(arr[i] < ` `0` `): ` ` ` `Len` `=` `max` `(` `Len` `,` `max` `(N ` `-` `i ` `-` `1` `, i)) ` ` ` ` ` `return` `Len` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `arr ` `=` `[` `-` `1` `, ` `1` `, ` `1` `, ` `-` `2` `, ` `3` `, ` `2` `, ` `-` `1` `] ` ` ` `N ` `=` `len` `(arr) ` ` ` ` ` `print` `(maxLength(arr, N)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG{ ` `// Function that count the length ` `// of longest subarray with product ` `// greater than or equals to zero ` ` ` `static` `int` `maxLength(` `int` `[]arr, ` `int` `N) ` ` ` `{ ` ` ` `int` `product = 1, len = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `product *= arr[i]; ` ` ` `} ` ` ` ` ` `// If product is greater than ` ` ` `// zero, return array size ` ` ` `if` `(product >= 0) { ` ` ` `return` `N; ` ` ` `} ` ` ` ` ` `// Traverse the array and if ` ` ` `// any negative element found ` ` ` `// then update the length of ` ` ` `// longest subarray with the ` ` ` `// length of left and right subarray ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `if` `(arr[i] < 0) { ` ` ` `len = Math.Max(len, Math.Max(N - i - 1, i)); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `len; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = { -1, 1, 1, -2, 3, 2, -1 }; ` ` ` `int` `N = arr.Length; ` ` ` `Console.WriteLine(maxLength(arr, N)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by abhaysingh290895 ` |

*chevron_right*

*filter_none*

**Output:**

6

* Time Complexity: O(N)*, where N is the length of the array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray of size greater than K with sum greater than a given value
- Longest subarray having average greater than or equal to x
- Longest Subarray with first element greater than or equal to Last element
- Longest subarray having average greater than or equal to x | Set-2
- Longest Subarray with Sum greater than Equal to Zero
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Length of longest subarray with product equal to a power of 2
- Longest subarray in which all elements are greater than K
- Longest subarray in which absolute difference between any two element is not greater than X
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Smallest subarray from a given Array with sum greater than or equal to K
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Smallest subarray from a given Array with sum greater than or equal to K | Set 2
- Length of longest subarray with negative product
- Length of longest subarray with positive product
- Highest and Smallest power of K less than and greater than equal to N respectively
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Maximum length of subarray such that all elements are equal in the subarray
- Length of longest subarray of length at least 2 with maximum GCD

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.