# Number of subsequences with positive product

Given an array arr[] of N integers, the task is to find the count of all the subsequences of the array that have positive product.

Example:

Input: arr[] = {2, -3, -1}
Output: 3
{2}, {-3, -1} and {2, -3, -1} are the only possible subsequences.

Input: arr[] = {2, 3, -1, 4, 5}
Output: 15

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: Generate all the subsequences of the array and compute the product of all the subsequences. If the product is positive then increment the count by 1.

Efficient approach:

1. Count the number of positive and negative elements in the array.
2. Any number of positive elements can be chosen for the subsequence to maintain the positive product. The number of different combinations of subsequences with all the positive elements will be pow(2, count of positive elements).
3. An even number of negative elements can be chosen for the subsequence to maintain the positive product. The number of different combinations of subsequences with an even number of negative elements will be pow(2, count of negative elements – 1).
4. After that, remove 1 from the results for the empty subsequence.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of all ` `// the subsequences with positive product ` `int` `cntSubSeq(``int` `arr[], ``int` `n) ` `{ ` `    ``// To store the count of positive ` `    ``// elements in the array ` `    ``int` `pos_count = 0; ` ` `  `    ``// To store the count of negative ` `    ``// elements in the array ` `    ``int` `neg_count = 0; ` ` `  `    ``int` `result; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// If the current element ` `        ``// is positive ` `        ``if` `(arr[i] > 0) ` `            ``pos_count++; ` ` `  `        ``// If the current element ` `        ``// is negative ` `        ``if` `(arr[i] < 0) ` `            ``neg_count++; ` `    ``} ` ` `  `    ``// For all the positive ` `    ``// elements of the array ` `    ``result = ``pow``(2, pos_count); ` ` `  `    ``// For all the negative ` `    ``// elements of the array ` `    ``if` `(neg_count > 0) ` `        ``result *= ``pow``(2, neg_count - 1); ` ` `  `    ``// For the empty subsequence ` `    ``result -= 1; ` ` `  `    ``return` `result; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, -3, -1, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``cout << cntSubSeq(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to return the count of all ` `// the subsequences with positive product ` `static` `int` `cntSubSeq(``int` `arr[], ``int` `n) ` `{ ` `    ``// To store the count of positive ` `    ``// elements in the array ` `    ``int` `pos_count = ``0``; ` ` `  `    ``// To store the count of negative ` `    ``// elements in the array ` `    ``int` `neg_count = ``0``; ` ` `  `    ``int` `result; ` ` `  `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` ` `  `        ``// If the current element ` `        ``// is positive ` `        ``if` `(arr[i] > ``0``) ` `            ``pos_count++; ` ` `  `        ``// If the current element ` `        ``// is negative ` `        ``if` `(arr[i] < ``0``) ` `            ``neg_count++; ` `    ``} ` ` `  `    ``// For all the positive ` `    ``// elements of the array ` `    ``result = (``int``) Math.pow(``2``, pos_count); ` ` `  `    ``// For all the negative ` `    ``// elements of the array ` `    ``if` `(neg_count > ``0``) ` `        ``result *= Math.pow(``2``, neg_count - ``1``); ` ` `  `    ``// For the empty subsequence ` `    ``result -= ``1``; ` ` `  `    ``return` `result; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``2``, -``3``, -``1``, ``4` `}; ` `    ``int` `n = arr.length; ` ` `  `    ``System.out.print(cntSubSeq(arr, n)); ` ` `  `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python 3 implementation of the approach  ` `import` `math ` ` `  `# Function to return the count of all  ` `# the subsequences with positive product  ` `def` `cntSubSeq(arr, n): ` ` `  `    ``# To store the count of positive  ` `    ``# elements in the array  ` `    ``pos_count ``=` `0``;  ` ` `  `    ``# To store the count of negative  ` `    ``# elements in the array  ` `    ``neg_count ``=` `0` ` `  `    ``for` `i ``in` `range` `(n): ` ` `  `        ``# If the current element  ` `        ``# is positive  ` `        ``if` `(arr[i] > ``0``) : ` `            ``pos_count ``+``=` `1` ` `  `        ``# If the current element  ` `        ``# is negative  ` `        ``if` `(arr[i] < ``0``):  ` `            ``neg_count ``+``=` `1` ` `  `    ``# For all the positive  ` `    ``# elements of the array  ` `    ``result ``=` `int``(math.``pow``(``2``, pos_count))  ` ` `  `    ``# For all the negative  ` `    ``# elements of the array  ` `    ``if` `(neg_count > ``0``): ` `        ``result ``*``=` `int``(math.``pow``(``2``, neg_count ``-` `1``))  ` ` `  `    ``# For the empty subsequence  ` `    ``result ``-``=` `1` ` `  `    ``return` `result ` ` `  `# Driver code  ` `arr ``=` `[ ``2``, ``-``3``, ``-``1``, ``4` `]  ` `n ``=` `len` `(arr);  ` ` `  `print` `(cntSubSeq(arr, n))  ` ` `  `# This code is contributed by ANKITKUMAR34 `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `    ``// Function to return the count of all ` `    ``// the subsequences with positive product ` `    ``static` `int` `cntSubSeq(``int` `[]arr, ``int` `n) ` `    ``{ ` `        ``// To store the count of positive ` `        ``// elements in the array ` `        ``int` `pos_count = 0; ` `     `  `        ``// To store the count of negative ` `        ``// elements in the array ` `        ``int` `neg_count = 0; ` `     `  `        ``int` `result; ` `     `  `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `     `  `            ``// If the current element ` `            ``// is positive ` `            ``if` `(arr[i] > 0) ` `                ``pos_count++; ` `     `  `            ``// If the current element ` `            ``// is negative ` `            ``if` `(arr[i] < 0) ` `                ``neg_count++; ` `        ``} ` `     `  `        ``// For all the positive ` `        ``// elements of the array ` `        ``result = (``int``) Math.Pow(2, pos_count); ` `     `  `        ``// For all the negative ` `        ``// elements of the array ` `        ``if` `(neg_count > 0) ` `            ``result *= (``int``)Math.Pow(2, neg_count - 1); ` `     `  `        ``// For the empty subsequence ` `        ``result -= 1; ` `     `  `        ``return` `result; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `[]arr = { 2, -3, -1, 4 }; ` `        ``int` `n = arr.Length; ` `     `  `        ``Console.Write(cntSubSeq(arr, n)); ` `     `  `    ``} ` `} ` ` `  `// This code is contributed by AnkitRai01 `

Output:

```7
```

Time Complexity: O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.