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 the 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
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:
- Count the number of positive and negative elements in the array.
- 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).
- 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).
- After that, remove 1 from the results for the empty subsequence.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntSubSeq( int arr[], int n)
{
int pos_count = 0;
int neg_count = 0;
int result;
for ( int i = 0; i < n; i++) {
if (arr[i] > 0)
pos_count++;
if (arr[i] < 0)
neg_count++;
}
result = pow (2, pos_count);
if (neg_count > 0)
result *= pow (2, neg_count - 1);
result -= 1;
return result;
}
int main()
{
int arr[] = { 2, -3, -1, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << cntSubSeq(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int cntSubSeq( int arr[], int n)
{
int pos_count = 0 ;
int neg_count = 0 ;
int result;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] > 0 )
pos_count++;
if (arr[i] < 0 )
neg_count++;
}
result = ( int ) Math.pow( 2 , pos_count);
if (neg_count > 0 )
result *= Math.pow( 2 , neg_count - 1 );
result -= 1 ;
return result;
}
public static void main(String[] args)
{
int arr[] = { 2 , - 3 , - 1 , 4 };
int n = arr.length;
System.out.print(cntSubSeq(arr, n));
}
}
|
Python3
import math
def cntSubSeq(arr, n):
pos_count = 0 ;
neg_count = 0
for i in range (n):
if (arr[i] > 0 ) :
pos_count + = 1
if (arr[i] < 0 ):
neg_count + = 1
result = int (math. pow ( 2 , pos_count))
if (neg_count > 0 ):
result * = int (math. pow ( 2 , neg_count - 1 ))
result - = 1
return result
arr = [ 2 , - 3 , - 1 , 4 ]
n = len (arr);
print (cntSubSeq(arr, n))
|
C#
using System;
class GFG
{
static int cntSubSeq( int []arr, int n)
{
int pos_count = 0;
int neg_count = 0;
int result;
for ( int i = 0; i < n; i++)
{
if (arr[i] > 0)
pos_count++;
if (arr[i] < 0)
neg_count++;
}
result = ( int ) Math.Pow(2, pos_count);
if (neg_count > 0)
result *= ( int )Math.Pow(2, neg_count - 1);
result -= 1;
return result;
}
public static void Main()
{
int []arr = { 2, -3, -1, 4 };
int n = arr.Length;
Console.Write(cntSubSeq(arr, n));
}
}
|
Javascript
<script>
function cntSubSeq(arr, n) {
let pos_count = 0;
let neg_count = 0;
let result;
for (let i = 0; i < n; i++) {
if (arr[i] > 0)
pos_count++;
if (arr[i] < 0)
neg_count++;
}
result = Math.pow(2, pos_count);
if (neg_count > 0)
result *= Math.pow(2, neg_count - 1);
result -= 1;
return result;
}
let arr = [2, -3, -1, 4];
let n = arr.length;
document.write(cntSubSeq(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
24 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...