Given an array arr[] consisting of N integers, the task is to print the length of the longest subarray with a positive product.
Examples:
Input: arr[] ={0, 1, -2, -3, -4}
Output: 3
Explanation:
The longest subarray with positive products is: {1, -2, -3}. Therefore, the required length is 3.
Input: arr[]={-1, -2, 0, 1, 2}
Output: 2
Explanation:
The longest subarray with positive products are: {-1, -2}, {1, 2}. Therefore, the required length is 2.
Naive Approach: The simplest approach to solve the problem is to generate all possible subarrays and check if its product is positive or not. Among all such subarrays, print the length of the longest subarray obtained.
Time Complexity: (N3)
Auxiliary Space: O(1)
Efficient Approach: The problem can be solved using Dynamic Programming. The idea here is to maintain the count of positive elements and negative elements such that their product is positive. Follow the steps below to solve the problem:
- Initialize the variable, say res, to store the length of the longest subarray with the positive product.
- Initialize two variables, Pos and Neg, to store the length of the current subarray with the positive and negative products respectively.
- Iterate over the array.
- If arr[i] = 0: Reset the value of Pos and Neg.
- If arr[i] > 0: Increment Pos by 1. If at least one element is present in the subarray with the negative product, then increment Neg by 1.
- If arr[i] < 0: Swap Pos and Neg and increment the Neg by 1. If at least one element is present in the subarray with the positive product, then increment Pos also.
- Update res=max(res, Pos).
C++
#include <bits/stdc++.h>
using namespace std;
int maxLenSub( int arr[], int N)
{
int Pos = 0;
int Neg = 0;
int res = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == 0) {
Pos = Neg = 0;
}
else if (arr[i] > 0) {
Pos += 1;
if (Neg != 0) {
Neg += 1;
}
res = max(res, Pos);
}
else {
swap(Pos, Neg);
Neg += 1;
if (Pos != 0) {
Pos += 1;
}
res = max(res, Pos);
}
}
return res;
}
int main()
{
int arr[] = { -1, -2, -3, 0, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxLenSub(arr, N);
}
|
Python3
def maxLenSub(arr, N):
Pos = 0
Neg = 0
res = 0
for i in range (N):
if (arr[i] = = 0 ):
Pos = Neg = 0
elif (arr[i] > 0 ):
Pos + = 1
if (Neg ! = 0 ):
Neg + = 1
res = max (res, Pos)
else :
Pos, Neg = Neg, Pos
Neg + = 1
if (Pos ! = 0 ):
Pos + = 1
res = max (res, Pos)
return res
if __name__ = = '__main__' :
arr = [ - 1 , - 2 , - 3 , 0 , 1 ]
N = len (arr)
print (maxLenSub(arr, N))
|
Java
import java.util.*;
class GFG{
static int maxLenSub( int arr[], int N)
{
int Pos = 0 ;
int Neg = 0 ;
int res = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == 0 )
{
Pos = Neg = 0 ;
}
else if (arr[i] > 0 )
{
Pos += 1 ;
if (Neg != 0 )
{
Neg += 1 ;
}
res = Math.max(res, Pos);
}
else
{
Pos = Pos + Neg;
Neg = Pos - Neg;
Pos = Pos - Neg;
Neg += 1 ;
if (Pos != 0 )
{
Pos += 1 ;
}
res = Math.max(res, Pos);
}
}
return res;
}
public static void main(String[] args)
{
int arr[] = {- 1 , - 2 , - 3 , 0 , 1 };
int N = arr.length;
System.out.print(maxLenSub(arr, N));
}
}
|
C#
using System;
class GFG{
static int maxLenSub( int [] arr, int N)
{
int Pos = 0;
int Neg = 0;
int res = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] == 0)
{
Pos = Neg = 0;
}
else if (arr[i] > 0)
{
Pos += 1;
if (Neg != 0)
{
Neg += 1;
}
res = Math.Max(res, Pos);
}
else
{
Pos = Pos + Neg;
Neg = Pos - Neg;
Pos = Pos - Neg;
Neg += 1;
if (Pos != 0)
{
Pos += 1;
}
res = Math.Max(res, Pos);
}
}
return res;
}
public static void Main()
{
int [] arr = {-1, -2, -3, 0, 1};
int N = arr.Length;
Console.Write(maxLenSub(arr, N));
}
}
|
Javascript
<script>
function maxLenSub(arr, N)
{
var Pos = 0;
var Neg = 0;
var res = 0;
for ( var i = 0; i < N; i++) {
if (arr[i] == 0) {
Pos = Neg = 0;
}
else if (arr[i] > 0) {
Pos += 1;
if (Neg != 0) {
Neg += 1;
}
res = Math.max(res, Pos);
}
else {
[Pos, Neg] = [Neg, Pos];
Neg += 1;
if (Pos != 0) {
Pos += 1;
}
res = Math.max(res, Pos);
}
}
return res;
}
var arr = [-1, -2, -3, 0, 1];
var N = arr.length;
document.write( maxLenSub(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)