Given an array ‘arr’ consisting of integers, the task is to find the number of subsets such that their sum is equal to zero. Empty subset should also be considered.
Examples:
Input : arr[] = {2, 2, -4}
Output : 2
All possible subsets:
{} = 0
{2} = 2
{2} = 2
{-4} = -4
{2, 2} = 4
{2, -4} = -2
{2, -4} = -4
{2, 2, -4} = 0
Since, {} and {2, 2, -4} are only possible subsets
with sum 0, ans will be 2.Input : arr[] = {1, 1, 1, 1}
Output : 1
{} is the only possible subset with
sum 0, thus ans equals 1.
One simple approach is to generate all possible subsets recursively and count number of subsets with sum equals 0. Time complexity of this approach will be O(2^n).
A better approach will be using Dynamic programming.
Let’s suppose sum of all the elements we have selected upto index ‘i-1’ is ‘S’. So, starting from index ‘i’, we have to find number of subsets of the sub-array{i, N-1} with sum equals -S.
Let’s define dp[i][S]. It means number of the subset of the subarray{i, N-1} of ‘arr’ with sum equals ‘-S’.
If we are at ith index, we have two choices, i.e. to include it in the sum or leave it.
Thus, the required recurrence relation becomes
dp[i][s] = dp[i+1][s+arr[i]] + dp[i+1][s]
Below is the implementation of above approach:
C++
#include <bits/stdc++.h> #define maxSum 100 #define arrSize 51 using namespace std; // variable to store // states of dp int dp[arrSize][maxSum]; bool visit[arrSize][maxSum]; // To find the number of subsets with sum equal to 0 // Since S can be negative, we will maxSum // to it to make it positive int SubsetCnt(int i, int s, int arr[], int n) { // Base cases if (i == n) { if (s == 0) return 1; else return 0; } // Returns the value if a state is already solved if (visit[i][s + maxSum]) return dp[i][s + maxSum]; // If the state is not visited, then continue visit[i][s + maxSum] = 1; // Recurrence relation dp[i][s + maxSum] = SubsetCnt(i + 1, s + arr[i], arr, n) + SubsetCnt(i + 1, s, arr, n); // Returning the value return dp[i][s + maxSum]; } // Driver function int main() { int arr[] = { 2, 2, 2, -4, -4 }; int n = sizeof(arr) / sizeof(int); cout << SubsetCnt(0, 0, arr, n); } |
Java
// Java implementation of above approach class GFG { static int maxSum = 100; static int arrSize = 51; // variable to store // states of dp static int[][] dp = new int[arrSize][maxSum]; static boolean[][] visit = new boolean[arrSize][maxSum]; // To find the number of subsets with sum equal to 0 // Since S can be negative, we will maxSum // to it to make it positive static int SubsetCnt(int i, int s, int arr[], int n) { // Base cases if (i == n) { if (s == 0) { return 1; } else { return 0; } } // Returns the value if a state is already solved if (visit[i][s + arrSize]) { return dp[i][s + arrSize]; } // If the state is not visited, then continue visit[i][s + arrSize] = true; // Recurrence relation dp[i][s + arrSize] = SubsetCnt(i + 1, s + arr[i], arr, n) + SubsetCnt(i + 1, s, arr, n); // Returning the value return dp[i][s + arrSize]; } // Driver function public static void main(String[] args) { int arr[] = {2, 2, 2, -4, -4}; int n = arr.length; System.out.println(SubsetCnt(0, 0, arr, n)); } } /* This code contributed by PrinciRaj1992 */ |
Python3
# Python3 implementation of above approach import numpy as np maxSum = 100arrSize = 51 # variable to store # states of dp dp = np.zeros((arrSize, maxSum)); visit = np.zeros((arrSize, maxSum)); # To find the number of subsets # with sum equal to 0. # Since S can be negative, # we will maxSum to it # to make it positive def SubsetCnt(i, s, arr, n) : # Base cases if (i == n) : if (s == 0) : return 1; else : return 0; # Returns the value # if a state is already solved if (visit[i][s + arrSize]) : return dp[i][s + arrSize]; # If the state is not visited, # then continue visit[i][s + arrSize] = 1; # Recurrence relation dp[i][s + arrSize ] = (SubsetCnt(i + 1, s + arr[i], arr, n) + SubsetCnt(i + 1, s, arr, n)); # Returning the value return dp[i][s + arrSize]; # Driver Code if __name__ == "__main__" : arr = [ 2, 2, 2, -4, -4 ]; n = len(arr); print(SubsetCnt(0, 0, arr, n)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of above approach using System; class GFG { static int maxSum = 100; static int arrSize = 51; // variable to store // states of dp static int [,]dp = new int[arrSize, maxSum]; static bool [,]visit = new bool[arrSize, maxSum]; // To find the number of subsets with sum equal to 0 // Since S can be negative, we will maxSum // to it to make it positive static int SubsetCnt(int i, int s, int []arr, int n) { // Base cases if (i == n) { if (s == 0) { return 1; } else { return 0; } } // Returns the value if a state is already solved if (visit[i, s + arrSize]) { return dp[i, s + arrSize]; } // If the state is not visited, then continue visit[i, s + arrSize] = true; // Recurrence relation dp[i, s + arrSize] = SubsetCnt(i + 1, s + arr[i], arr, n) + SubsetCnt(i + 1, s, arr, n); // Returning the value return dp[i,s + arrSize]; } // Driver code public static void Main() { int []arr = {2, 2, 2, -4, -4}; int n = arr.Length; Console.WriteLine(SubsetCnt(0, 0, arr, n)); } } // This code contributed by anuj_67.. |
7
Time Complexity : O(n*S), where n is the number of elements in the array and S is the sum of all the elements.
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.

