Skip to content
Related Articles

Related Articles

Length of longest subarray with product greater than or equal to 0
  • Last Updated : 12 Apr, 2021

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:
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:
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++




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

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

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

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

Javascript




<script>
 
// Javascript implementation of the above approach
 
// Function that count the length
// of longest subarray with product
// greater than or equals to zero
function maxLength(arr, N)
{
    var product = 1, len = 0;
 
    for (var 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 (var i = 0; i < N; i++) {
        if (arr[i] < 0) {
            len = Math.max(len,
                      Math.max(N - i - 1, i));
        }
    }
 
    return len;
}
 
// Driver Code
var arr = [ -1, 1, 1, -2, 3, 2, -1 ];
var N = arr.length;
document.write(maxLength(arr, N));
 
// This code is contributed by rutvik_56.
</script>
Output: 
6

 

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :