Length of longest subarray with product greater than or equal to 0

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:



  1. Check whether the product of all the elements in the given array is greater than or equals zero or not.
  2. If Yes then, the length of the longest subarray with a product greater than or equals to zero is the length of the array.
  3. 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.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

6

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.