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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count of // subarrays with negative product int negProdSubArr( int arr[], int n) { int positive = 1, negative = 0; for ( int i = 0; i < n; i++) { // Replace current element with 1 // if it is positive else replace // it with -1 instead if (arr[i] > 0) arr[i] = 1; else arr[i] = -1; // Take product with previous element // to form the prefix product if (i > 0) arr[i] *= arr[i - 1]; // Count positive and negative elements // in the prefix product array if (arr[i] == 1) positive++; else negative++; } // Return the required count of subarrays return (positive * negative); } // Function to return the count of // subarrays with positive product int posProdSubArr( int arr[], int n) { // Total subarrays possible int total = (n * (n + 1)) / 2; // Count to subarrays with negative product int cntNeg = negProdSubArr(arr, n); // Return the count of subarrays // with positive product return (total - cntNeg); } // Driver code int main() { int arr[] = { 5, -4, -3, 2, -5 }; int n = sizeof (arr) / sizeof (arr[0]); cout << posProdSubArr(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the count of // subarrays with negative product static int negProdSubArr( int arr[], int n) { int positive = 1 , negative = 0 ; for ( int i = 0 ; i < n; i++) { // Replace current element with 1 // if it is positive else replace // it with -1 instead if (arr[i] > 0 ) arr[i] = 1 ; else arr[i] = - 1 ; // Take product with previous element // to form the prefix product if (i > 0 ) arr[i] *= arr[i - 1 ]; // Count positive and negative elements // in the prefix product array if (arr[i] == 1 ) positive++; else negative++; } // Return the required count of subarrays return (positive * negative); } // Function to return the count of // subarrays with positive product static int posProdSubArr( int arr[], int n) { // Total subarrays possible int total = (n * (n + 1 )) / 2 ; // Count to subarrays with negative product int cntNeg = negProdSubArr(arr, n); // Return the count of subarrays // with positive product return (total - cntNeg); } // Driver code public static void main (String[] args) { int arr[] = { 5 , - 4 , - 3 , 2 , - 5 }; int n = arr.length; System.out.println(posProdSubArr(arr, n)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to return the count of # subarrays with negative product def negProdSubArr(arr, n): positive = 1 negative = 0 for i in range (n): # Replace current element with 1 # if it is positive else replace # it with -1 instead if (arr[i] > 0 ): arr[i] = 1 else : arr[i] = - 1 # Take product with previous element # to form the prefix product if (i > 0 ): arr[i] * = arr[i - 1 ] # Count positive and negative elements # in the prefix product array if (arr[i] = = 1 ): positive + = 1 else : negative + = 1 # Return the required count of subarrays return (positive * negative) # Function to return the count of # subarrays with positive product def posProdSubArr(arr, n): total = (n * (n + 1 )) / 2 ; # Count to subarrays with negative product cntNeg = negProdSubArr(arr, n); # Return the count of subarrays # with positive product return (total - cntNeg); # Driver code arr = [ 5 , - 4 , - 3 , 2 , - 5 ] n = len (arr) print (posProdSubArr(arr, n)) # This code is contributed by Mehul Bhutalia |
C#
// C# implementation of the approach using System; class GFG { // Function to return the count of // subarrays with negative product static int negProdSubArr( int []arr, int n) { int positive = 1, negative = 0; for ( int i = 0; i < n; i++) { // Replace current element with 1 // if it is positive else replace // it with -1 instead if (arr[i] > 0) arr[i] = 1; else arr[i] = -1; // Take product with previous element // to form the prefix product if (i > 0) arr[i] *= arr[i - 1]; // Count positive and negative elements // in the prefix product array if (arr[i] == 1) positive++; else negative++; } // Return the required count of subarrays return (positive * negative); } // Function to return the count of // subarrays with positive product static int posProdSubArr( int []arr, int n) { // Total subarrays possible int total = (n * (n + 1)) / 2; // Count to subarrays with negative product int cntNeg = negProdSubArr(arr, n); // Return the count of subarrays // with positive product return (total - cntNeg); } // Driver code public static void Main (String[] args) { int []arr = { 5, -4, -3, 2, -5 }; int n = arr.Length; Console.WriteLine(posProdSubArr(arr, n)); } } // This code is contributed by 29AjayKumar |
7
Recommended Posts:
- Number of subarrays having product less than K
- Number of subarrays with given product
- Number of subarrays for which product and sum are equal
- Kth Smallest sum of continuous subarrays of positive numbers
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Check whether product of integers from a to b is positive , negative or zero
- Minimum product pair an array of positive Integers
- Check if the product of the K largest sums of subarrays is greater than M
- Find an element which divides the array in two subarrays with equal product
- Minimum product of k integers in an array of positive Integers
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Number of subarrays having sum less than K
- Number of subarrays have bitwise OR >= K
- Count the number of subarrays having a given XOR
- Number of subarrays with m odd numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.