Number of subsets with zero sum |  ; variable to store states of dp ; 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 ; Base cases ; Returns the value if a state is already solved ; If the state is not visited , then continue ; Recurrence relation ; Returning the value ; Driver function
var maxSum = 100 var arrSize = 51 var dp = Array . from ( Array ( arrSize ) , ( ) => Array ( maxSum ) ) ; var visit = Array . from ( Array ( arrSize ) , ( ) => Array ( maxSum ) ) ; function SubsetCnt ( i , s , arr , n ) { if ( i == n ) { if ( s == 0 ) return 1 ; else return 0 ; } if ( visit [ i ] [ s + maxSum ] ) return dp [ i ] [ s + maxSum ] ; visit [ i ] [ s + maxSum ] = 1 ; dp [ i ] [ s + maxSum ] = SubsetCnt ( i + 1 , s + arr [ i ] , arr , n ) + SubsetCnt ( i + 1 , s , arr , n ) ; return dp [ i ] [ s + maxSum ] ; } var arr = [ 2 , 2 , 2 , - 4 , - 4 ] ; var n = arr . length ; document . write ( SubsetCnt ( 0 , 0 , arr , n ) ) ;