📜  阳性乘积的子序列数

📅  最后修改于: 2021-04-21 23:18:28             🧑  作者: Mango

给定N个整数的数组arr [] ,任务是查找具有正积的数组所有子序列的计数。

例子:

天真的方法:生成数组的所有子序列,并计算所有子序列的乘积。如果乘积为正,则将计数加1

高效的方法:

  1. 计算数组中正负元素的数量。
  2. 可以为子序列选择任意数量的阳性元素以维持阳性乘积。具有所有正元素的子序列不同组合的数量将为pow(2,正元素计数)
  3. 可以为子序列选择偶数个负元素,以维持正乘积。具有负数个偶数个负数的子序列的不同组合的数量将为pow(2,负数个数– 1)
  4. 之后,从结果中为空子序列删除1

下面是上述方法的实现:

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[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 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


输出:
7

时间复杂度: O(n)