Count of subsets with sum equal to X using Recursion | Recursive function to return the count of subsets with sum equal to the given value ; The recursion is stopped at N - th level where all the subsets of the given array have been checked ; Incrementing the count if sum is equal to 0 and returning the count ; Recursively calling the function for two cases Either the element can be counted in the subset If the element is counted , then the remaining sum to be checked is sum - the selected element If the element is not included , then the remaining sum to be checked is the total sum ; Driver code
def subsetSum ( arr , n , i , sum , count ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT if ( sum == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) NEW_LINE count = subsetSum ( arr , n , i + 1 , sum , count ) NEW_LINE return count NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE sum = 10 NEW_LINE n = len ( arr ) NEW_LINE print ( subsetSum ( arr , n , 0 , sum , 0 ) ) NEW_LINE