Count of subsets with sum equal to X using Recursion | Java program to print the count of subsets with sum equal to the given value X ; 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 the subset and returning the count ; Driver code ; int s = 0 ; Initially assigning the sum of subset to be zero
import java . util . * ; class GFG { static int subsetSum ( int arr [ ] , int n , int sum , int s , int count ) { if ( n == 0 ) { if ( sum == s ) { count ++ ; } return count ; } count = subsetSum ( arr , n - 1 , sum , s , count ) ; count = subsetSum ( arr , n - 1 , sum , s + arr [ n - 1 ] , count ) ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int sum = 10 ; int n = arr . length ; System . out . print ( subsetSum ( arr , n , sum , s , 0 ) ) ; } }