📜  总和为零的子序列数

📅  最后修改于: 2021-06-25 15:13:53             🧑  作者: Mango

给定N个整数的数组arr [] 。任务是计算总和为0的子序列数。
例子:

方法:可以使用递归解决问题。递归地,我们从第一个索引开始,然后选择要在子序列中添加的数字,或者不选择索引处的数字。一旦索引超过N,我们需要检查求和的总和是否为0,并且子序列中的数字计数应至少为1。如果是,那么我们仅返回1,它被添加到路数中。
由于总和值可能无法存储在任何维数组中,因此无法使用动态编程来解决此问题。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count
// of the required sub-sequences
int countSubSeq(int i, int sum, int cnt,
                int a[], int n)
{
 
    // Base case
    if (i == n) {
 
        // Check if the sum is 0
        // and at least a single element
        // is in the sub-sequence
        if (sum == 0 && cnt > 0)
            return 1;
        else
            return 0;
    }
    int ans = 0;
 
    // Do not take the number in
    // the current sub-sequence
    ans += countSubSeq(i + 1, sum, cnt, a, n);
 
    // Take the number in the
    // current sub-sequence
    ans += countSubSeq(i + 1, sum + a[i],
                       cnt + 1, a, n);
 
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { -1, 2, -2, 1 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << countSubSeq(0, 0, 0, a, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
    // Function to return the count
    // of the required sub-sequences
    static int countSubSeq(int i, int sum, int cnt,
                                    int a[], int n)
    {
 
        // Base case
        if (i == n)
        {
 
            // Check if the sum is 0
            // and at least a single element
            // is in the sub-sequence
            if (sum == 0 && cnt > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        int ans = 0;
 
        // Do not take the number in
        // the current sub-sequence
        ans += countSubSeq(i + 1, sum, cnt, a, n);
 
        // Take the number in the
        // current sub-sequence
        ans += countSubSeq(i + 1, sum + a[i],
                                cnt + 1, a, n);
 
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = {-1, 2, -2, 1};
        int n = a.length;
        System.out.println(countSubSeq(0, 0, 0, a, n));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
 
# Function to return the count
# of the required sub-sequences
def countSubSeq(i, Sum, cnt, a, n):
 
    # Base case
    if (i == n):
 
        # Check if the Sum is 0
        # and at least a single element
        # is in the sub-sequence
        if (Sum == 0 and cnt > 0):
            return 1
        else:
            return 0
    ans = 0
 
    # Do not take the number in
    # the current sub-sequence
    ans += countSubSeq(i + 1, Sum, cnt, a, n)
 
    # Take the number in the
    # current sub-sequence
    ans += countSubSeq(i + 1, Sum + a[i],
                           cnt + 1, a, n)
 
    return ans
 
# Driver code
a = [-1, 2, -2, 1]
n = len(a)
print(countSubSeq(0, 0, 0, a, n))
 
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the count
    // of the required sub-sequences
    static int countSubSeq(int i, int sum,
                           int cnt, int []a, int n)
    {
 
        // Base case
        if (i == n)
        {
 
            // Check if the sum is 0
            // and at least a single element
            // is in the sub-sequence
            if (sum == 0 && cnt > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
         
        int ans = 0;
 
        // Do not take the number in
        // the current sub-sequence
        ans += countSubSeq(i + 1, sum, cnt, a, n);
 
        // Take the number in the
        // current sub-sequence
        ans += countSubSeq(i + 1, sum + a[i],
                                  cnt + 1, a, n);
 
        return ans;
    }
 
    // Driver code
    public static void Main()
    {
        int []a = {-1, 2, -2, 1};
        int n = a.Length;
        Console.Write(countSubSeq(0, 0, 0, a, n));
    }
}
 
// This code is contributed by Akanksha Rai


PHP
 0)
            return 1;
        else
            return 0;
    }
    $ans = 0;
 
    // Do not take the number in
    // the current sub-sequence
    $ans += countSubSeq($i + 1, $sum,
                        $cnt, $a, $n);
 
    // Take the number in the
    // current sub-sequence
    $ans += countSubSeq($i + 1, $sum + $a[$i],
                        $cnt + 1, $a, $n);
 
    return $ans;
}
 
// Driver code
$a = array( -1, 2, -2, 1 );
$n = count($a) ;
 
echo countSubSeq(0, 0, 0, $a, $n);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
3