Number of subsequences with zero sum
Given an array arr[] of N integers. The task is to count the number of sub-sequences whose sum is 0.
Examples:
Input: arr[] = {-1, 2, -2, 1}
Output: 3
All possible sub-sequences are {-1, 1}, {2, -2} and {-1, 2, -2, 1}
Input: arr[] = {-2, -4, -1, 6, -2}
Output: 2
Approach: The problem can be solved using recursion. Recursively, we start from the first index, and either select the number to be added in the subsequence or we do not select the number at an index. Once the index exceeds N, we need to check if the sum evaluated is 0 or not and the count of numbers taken in subsequence should be a minimum of one. If it is, then we simply return 1 which is added to the number of ways.
Dynamic Programming cannot be used to solve this problem because of the sum value which can be anything that is not possible to store in any dimensional array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubSeq( int i, int sum, int cnt,
int a[], int n)
{
if (i == n) {
if (sum == 0 && cnt > 0)
return 1;
else
return 0;
}
int ans = 0;
ans += countSubSeq(i + 1, sum, cnt, a, n);
ans += countSubSeq(i + 1, sum + a[i],
cnt + 1, a, n);
return ans;
}
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
class GFG
{
static int countSubSeq( int i, int sum, int cnt,
int a[], int n)
{
if (i == n)
{
if (sum == 0 && cnt > 0 )
{
return 1 ;
}
else
{
return 0 ;
}
}
int ans = 0 ;
ans += countSubSeq(i + 1 , sum, cnt, a, n);
ans += countSubSeq(i + 1 , sum + a[i],
cnt + 1 , a, n);
return ans;
}
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));
}
}
|
Python3
def countSubSeq(i, Sum , cnt, a, n):
if (i = = n):
if ( Sum = = 0 and cnt > 0 ):
return 1
else :
return 0
ans = 0
ans + = countSubSeq(i + 1 , Sum , cnt, a, n)
ans + = countSubSeq(i + 1 , Sum + a[i],
cnt + 1 , a, n)
return ans
a = [ - 1 , 2 , - 2 , 1 ]
n = len (a)
print (countSubSeq( 0 , 0 , 0 , a, n))
|
C#
using System;
class GFG
{
static int countSubSeq( int i, int sum,
int cnt, int []a, int n)
{
if (i == n)
{
if (sum == 0 && cnt > 0)
{
return 1;
}
else
{
return 0;
}
}
int ans = 0;
ans += countSubSeq(i + 1, sum, cnt, a, n);
ans += countSubSeq(i + 1, sum + a[i],
cnt + 1, a, n);
return ans;
}
public static void Main()
{
int []a = {-1, 2, -2, 1};
int n = a.Length;
Console.Write(countSubSeq(0, 0, 0, a, n));
}
}
|
PHP
<?php
function countSubSeq( $i , $sum , $cnt , $a , $n )
{
if ( $i == $n )
{
if ( $sum == 0 && $cnt > 0)
return 1;
else
return 0;
}
$ans = 0;
$ans += countSubSeq( $i + 1, $sum ,
$cnt , $a , $n );
$ans += countSubSeq( $i + 1, $sum + $a [ $i ],
$cnt + 1, $a , $n );
return $ans ;
}
$a = array ( -1, 2, -2, 1 );
$n = count ( $a ) ;
echo countSubSeq(0, 0, 0, $a , $n );
?>
|
Javascript
<script>
function countSubSeq(i, sum, cnt, a, n)
{
if (i == n) {
if (sum == 0 && cnt > 0)
return 1;
else
return 0;
}
let ans = 0;
ans += countSubSeq(i + 1, sum, cnt, a, n);
ans += countSubSeq(i + 1, sum + a[i],
cnt + 1, a, n);
return ans;
}
let a = [ -1, 2, -2, 1 ];
let n = a.length;
document.write(countSubSeq(0, 0, 0, a, n));
</script>
|
Time Complexity: O(2N), as we are using recursion and T(N) = O(1) + 2*T(N-1) which will be equivalent to T(N) = (2^N – 1)*O(1). Where N is the number of elements in the array.
Auxiliary Space: O(N), where N is the recursion stack space.
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...