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: 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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxLength( int arr[], int N)
{
int product = 1, len = 0;
for ( int i = 0; i < N; i++) {
product *= arr[i];
}
if (product >= 0) {
return N;
}
for ( int i = 0; i < N; i++) {
if (arr[i] < 0) {
len = max(len,
max(N - i - 1, i));
}
}
return len;
}
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
import java.util.*;
public class GFG{
static int maxLength( int arr[], int N)
{
int product = 1 , len = 0 ;
for ( int i = 0 ; i < N; i++) {
product *= arr[i];
}
if (product >= 0 ) {
return N;
}
for ( int i = 0 ; i < N; i++) {
if (arr[i] < 0 ) {
len = Math.max(len, Math.max(N - i - 1 , i));
}
}
return len;
}
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));
}
}
|
Python3
def maxLength(arr, N):
product = 1
Len = 0
for i in arr:
product * = i
if (product > = 0 ):
return N
for i in range (N):
if (arr[i] < 0 ):
Len = max ( Len , max (N - i - 1 , i))
return Len
if __name__ = = '__main__' :
arr = [ - 1 , 1 , 1 , - 2 , 3 , 2 , - 1 ]
N = len (arr)
print (maxLength(arr, N))
|
C#
using System;
class GFG{
static int maxLength( int []arr, int N)
{
int product = 1, len = 0;
for ( int i = 0; i < N; i++) {
product *= arr[i];
}
if (product >= 0) {
return N;
}
for ( int i = 0; i < N; i++) {
if (arr[i] < 0) {
len = Math.Max(len, Math.Max(N - i - 1, i));
}
}
return len;
}
public static void Main()
{
int []arr = { -1, 1, 1, -2, 3, 2, -1 };
int N = arr.Length;
Console.WriteLine(maxLength(arr, N));
}
}
|
Javascript
<script>
function maxLength(arr, N)
{
var product = 1, len = 0;
for ( var i = 0; i < N; i++) {
product *= arr[i];
}
if (product >= 0) {
return N;
}
for ( var i = 0; i < N; i++) {
if (arr[i] < 0) {
len = Math.max(len,
Math.max(N - i - 1, i));
}
}
return len;
}
var arr = [ -1, 1, 1, -2, 3, 2, -1 ];
var N = arr.length;
document.write(maxLength(arr, N));
</script>
|
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...