Length of longest subarray with negative product
Given an array arr[] of N elements. The task is to find the length of the longest subarray such that the product of the subarray is negative. If there is no such subarray available, print -1.
Examples:
Input: N = 6, arr[] = {-1, 2, 3, 2, 1, -4}
Output: 5
Explanation:
In the example the subarray
in range [1, 5] has product -12 which is negative,
so the length is 5.
Input: N = 4, arr[] = {1, 2, 3, 2}
Output: -1
Approach:
- First, check if the total product of the array is negative. If the total product of the array is negative then the answer will be N.
- If the total product of the array is not negative, means it is positive. So, the idea is to find a negative element from the array such that excluding that element and comparing the length of both parts of the array we can obtain the max length of the subarray with the negative product.
- It is obvious that the-
subarray with negative product will exist in the range [1, x) or (x, N], where 1 <= x <= N, and arr[x] is negative.
Below is the implementation of the above approach-
C++
#include <bits/stdc++.h>
using namespace std;
int maxLength( int a[], int n)
{
int product = 1, len = -1;
for ( int i = 0; i < n; i++)
product *= a[i];
if (product < 0)
return n;
for ( int i = 0; i < n; i++) {
if (a[i] < 0)
len = max(len,
max(n - i - 1, i));
}
return len;
}
int main()
{
int arr[] = { 1, 2, -3, 2, 5, -6 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxLength(arr, N)
<< "\n" ;
int arr1[] = { 1, 2, 3, 4 };
N = sizeof (arr1) / sizeof (arr1[0]);
cout << maxLength(arr1, N)
<< "\n" ;
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static int maxLength( int a[], int n)
{
int product = 1 , len = - 1 ;
for ( int i = 0 ; i < n; i++)
product *= a[i];
if (product < 0 )
return n;
for ( int i = 0 ; i < n; i++)
{
if (a[i] < 0 )
len = Math.max(len,
Math.max(n - i - 1 , i));
}
return len;
}
public static void main (String[] args)
{
int arr[] = new int []{ 1 , 2 , - 3 ,
2 , 5 , - 6 };
int N = arr.length;
System.out.println(maxLength(arr, N));
int arr1[] = new int []{ 1 , 2 , 3 , 4 };
N = arr1.length;
System.out.println(maxLength(arr1, N));
}
}
|
Python3
def maxLength(a, n):
product = 1
length = - 1
for i in range (n):
product * = a[i]
if (product < 0 ):
return n
for i in range (n):
if (a[i] < 0 ):
length = max (length,
max (n - i - 1 , i))
return length
if __name__ = = "__main__" :
arr = [ 1 , 2 , - 3 , 2 , 5 , - 6 ]
N = len (arr)
print (maxLength(arr, N))
arr1 = [ 1 , 2 , 3 , 4 ]
N = len (arr1)
print (maxLength(arr1, N))
|
C#
using System;
class GFG{
static int maxLength( int []a, int n)
{
int product = 1, len = -1;
for ( int i = 0; i < n; i++)
product *= a[i];
if (product < 0)
return n;
for ( int i = 0; i < n; i++)
{
if (a[i] < 0)
len = Math.Max(len,
Math.Max(n - i - 1, i));
}
return len;
}
public static void Main(String[] args)
{
int []arr = new int []{ 1, 2, -3,
2, 5, -6 };
int N = arr.Length;
Console.WriteLine(maxLength(arr, N));
int []arr1 = new int []{ 1, 2, 3, 4 };
N = arr1.Length;
Console.WriteLine(maxLength(arr1, N));
}
}
|
Javascript
<script>
function maxLength(a, n)
{
let product = 1, len = -1;
for (let i = 0; i < n; i++)
product *= a[i];
if (product < 0)
return n;
for (let i = 0; i < n; i++)
{
if (a[i] < 0)
len = Math.max(len,
Math.max(n - i - 1, i));
}
return len;
}
let arr = [ 1, 2, -3,
2, 5, -6 ];
let N = arr.length;
document.write(maxLength(arr, N) + "<br/>" );
let arr1 = [ 1, 2, 3, 4 ];
N = arr1.Length;
document.write(maxLength(arr1, N) + "<br/>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
28 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...