Number of subsequences with negative product | C ++ implementation of the approach ; Function to return the count of all the subsequences with negative product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ) ; else result = 0 ; return result ; } int main ( ) { int arr [ ] = { 3 , -4 , -1 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << cntSubSeq ( arr , n ) ; return 0 ; }