Number of Subarrays with positive product
Given an array arr[] of N integers, the task is to find the count of subarrays with positive product.
Examples:
Input: arr[] = {-1, 2, -2}
Output: 2
Subarrays with positive product are {2} and {-1, 2, -2}.
Input: arr[] = {5, -4, -3, 2, -5}
Output: 7
Approach: The approach to find the subarrays with negative product has been discussed in this article. If cntNeg is the count of negative product subarrays and total is the count of all possible subarrays of the given array then the count of positive product subarrays will be cntPos = total – cntNeg.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int negProdSubArr( int arr[], int n)
{
int positive = 1, negative = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] > 0)
arr[i] = 1;
else
arr[i] = -1;
if (i > 0)
arr[i] *= arr[i - 1];
if (arr[i] == 1)
positive++;
else
negative++;
}
return (positive * negative);
}
int posProdSubArr( int arr[], int n)
{
int total = (n * (n + 1)) / 2;
int cntNeg = negProdSubArr(arr, n);
return (total - cntNeg);
}
int main()
{
int arr[] = { 5, -4, -3, 2, -5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << posProdSubArr(arr, n);
return 0;
}
|
Java
class GFG
{
static int negProdSubArr( int arr[], int n)
{
int positive = 1 , negative = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] > 0 )
arr[i] = 1 ;
else
arr[i] = - 1 ;
if (i > 0 )
arr[i] *= arr[i - 1 ];
if (arr[i] == 1 )
positive++;
else
negative++;
}
return (positive * negative);
}
static int posProdSubArr( int arr[], int n)
{
int total = (n * (n + 1 )) / 2 ;
int cntNeg = negProdSubArr(arr, n);
return (total - cntNeg);
}
public static void main (String[] args)
{
int arr[] = { 5 , - 4 , - 3 , 2 , - 5 };
int n = arr.length;
System.out.println(posProdSubArr(arr, n));
}
}
|
Python3
def negProdSubArr(arr, n):
positive = 1
negative = 0
for i in range (n):
if (arr[i] > 0 ):
arr[i] = 1
else :
arr[i] = - 1
if (i > 0 ):
arr[i] * = arr[i - 1 ]
if (arr[i] = = 1 ):
positive + = 1
else :
negative + = 1
return (positive * negative)
def posProdSubArr(arr, n):
total = (n * (n + 1 )) / 2 ;
cntNeg = negProdSubArr(arr, n);
return (total - cntNeg);
arr = [ 5 , - 4 , - 3 , 2 , - 5 ]
n = len (arr)
print (posProdSubArr(arr, n))
|
C#
using System;
class GFG
{
static int negProdSubArr( int []arr, int n)
{
int positive = 1, negative = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] > 0)
arr[i] = 1;
else
arr[i] = -1;
if (i > 0)
arr[i] *= arr[i - 1];
if (arr[i] == 1)
positive++;
else
negative++;
}
return (positive * negative);
}
static int posProdSubArr( int []arr, int n)
{
int total = (n * (n + 1)) / 2;
int cntNeg = negProdSubArr(arr, n);
return (total - cntNeg);
}
public static void Main (String[] args)
{
int []arr = { 5, -4, -3, 2, -5 };
int n = arr.Length;
Console.WriteLine(posProdSubArr(arr, n));
}
}
|
Javascript
<script>
function negProdSubArr(arr, n)
{
let positive = 1, negative = 0;
for (let i = 0; i < n; i++)
{
if (arr[i] > 0)
arr[i] = 1;
else
arr[i] = -1;
if (i > 0)
arr[i] *= arr[i - 1];
if (arr[i] == 1)
positive++;
else
negative++;
}
return (positive * negative);
}
function posProdSubArr(arr, n)
{
let total = parseInt((n * (n + 1)) / 2);
let cntNeg = negProdSubArr(arr, n);
return (total - cntNeg);
}
let arr = [ 5, -4, -3, 2, -5 ];
let n = arr.length;
document.write(posProdSubArr(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
19 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...