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 which is not possible to store in any dimensional array.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the count ` `// of the required sub-sequences ` `function` `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 && ` `$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` `= ` `array` `( -1, 2, -2, 1 ); ` `$n` `= ` `count` `(` `$a` `) ; ` ` ` `echo` `countSubSeq(0, 0, 0, ` `$a` `, ` `$n` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of all subsequences of a number
- Number of Subsequences with Even and Odd Sum
- Number of Subsequences with Even and Odd Sum | Set 2
- Number of K length subsequences with minimum sum
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Number of subsets with zero sum
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Sum of all subsequences of an array
- Subsequences of size three in an array whose sum is divisible by m
- Sum of width (max and min diff) of all Subsequences
- Find all combinations of two equal sum subsequences
- Find all subsequences with sum equals to K
- Count of subsequences in an array with sum less than or equal to X
- Sum of all subsequences of length K
- Unique subsequences of length K with given sum
- Minimize sum of smallest elements from K subsequences of length L
- Check if an array can be split into 3 subsequences of equal sum or not
- Different ways to represent N as sum of K non-zero integers
- Subset with sum closest to zero

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.