Wednesday, July 3, 2024
HomeData ModellingDynamic ProgrammingNumber of subsequences with negative product

Number of subsequences with negative product

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

Examples: 

Input: arr[] = {1, -5, -6} 
Output:
Explanation 
{-5}, {-6}, {1, -5} and {1, -6} are the only possible subsequences

Input: arr[] = {2, 3, 1} 
Output:
Explanation 
There is no such possible subsequence with negative product 
 

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

Efficient Approach:  

  • Count the number of positive and negative elements in the array
  • An odd number of negative elements can be chosen for the subsequence to maintain the negative product. The number of different combinations of subsequences with an odd number of negative elements will be pow(2, count of negative elements – 1)
  • Any number of positive elements can be chosen for the subsequence to maintain the negative product. The number of different combinations of subsequences with all the positive elements will be pow(2, count of positive elements)

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the count of all
// the subsequences with negative 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);
    else
        result = 0;
 
    return result;
}
 
// Driver code
int main()
{
    int arr[] = { 3, -4, -1, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    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 negative 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);
    else
        result = 0 ;
 
    return result;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3,-4, -1, 6 };
    int n = arr.length;
 
    System.out.print(cntSubSeq(arr, n));
}
}
 
// This code is contributed by ANKITKUMAR34


Python3




# Python 3 implementation of the approach
import math
 
# Function to return the count of all
# the subsequences with negative 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))
    else:
        result = 0
 
    return result
 
# Driver code
arr = [ 2, -3, -1, 4 ]
n = len (arr);
 
print (cntSubSeq(arr, n))


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the count of all
// the subsequences with negative 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);
    else
        result = 0 ;
 
    return result;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 3,-4, -1, 6 };
    int n = arr.Length;
 
    Console.Write(cntSubSeq(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the count of all
// the subsequences with negative product
function cntSubSeq(arr, n)
{
     
    // To store the count of positive
    // elements in the array
    var pos_count = 0;
 
    // To store the count of negative
    // elements in the array
    var neg_count = 0;
 
    var result;
 
    for(var 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 = Math.pow(2, pos_count);
 
    // For all the negative
    // elements of the array
    if (neg_count > 0)
        result *= Math.pow(2, neg_count - 1);
    else
        result = 0;
 
    return result;
}
 
// Driver code
var arr = [ 3, -4, -1, 6 ];
var n = arr.length;
 
document.write(cntSubSeq(arr, n));
 
// This code is contributed by noob2000
 
</script>


Output: 

8

 

Time Complexity: O(n)

Auxiliary Space: O(1)
Another Approach: 
We can also count the number of subsequences with a negative product by subtracting total number of subsequences with positive subsequences from the total number of subsequences
To find the total number of subsequences with a positive product using the approach discussed in this article.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments