Number of sub arrays with negative product
Last Updated :
21 Mar, 2023
Given an array arr[] of N integers, the task is to find the count of subarrays with negative product.
Examples:
Input: arr[] = {-1, 2, -2}
Output: 4
Subarray with negative product are {-1}, {-2}, {-1, 2} and {2, -2}.
Input: arr[] = {5, -4, -3, 2, -5}
Output: 8
Approach:
- Replace the positive array elements with 1 and negative array elements with -1.
- Create a prefix product array pre[] where pre[i] stores the product of all the elements from index arr[0] to arr[i].
- Now, it can be noted that the sub-array arr[i…j] has a negative product only if pre[i] * pre[j] is negative.
- Hence, the total count of sub-arrays with negative product will be the product of the count positive and negative elements in the prefix product array.
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 main()
{
int arr[] = { 5, -4, -3, 2, -5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << negProdSubArr(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);
}
public static void main (String[] args)
{
int arr[] = { 5 , - 4 , - 3 , 2 , - 5 };
int n = arr.length;
System.out.println(negProdSubArr(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)
arr = [ 5 , - 4 , - 3 , 2 , - 5 ]
n = len (arr)
print (negProdSubArr(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 public void Main ()
{
int []arr = { 5, -4, -3, 2, -5 };
int n = arr.Length;
Console.Write(negProdSubArr(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);
}
let arr = [ 5, -4, -3, 2, -5 ];
let n = arr.length;
document.write(negProdSubArr(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...