class Solution { /** Recursive code */ static Boolean isSubsetSum(int arr[], int sum) { int n = arr.length; return findSubsetSum(arr, sum, n); } static Boolean findSubsetSum(int[] arr, int sum, int n) { // when we reach end of the array, so couldn't found a subset if(n == 0) return false; // when sum becomes 0 that means found a subset if(sum == 0) return true; if(arr[n-1] <= sum) { // pick the element in subset boolean pick = findSubsetSum(arr, sum - arr[n-1], n-1); // do not pick the element in subset boolean notPick = findSubsetSum(arr, sum, n-1); return pick || notPick; } // when current element exceeds given sum, so skip the element return findSubsetSum(arr, sum, n-1); } /** Memoization code */ static Boolean isSubsetSum(int arr[], int sum) { int n = arr.length; boolean[][] t = new boolean[n+1][sum+1]; // when array of size will increase but sum needs to always 0, then we can have always empty subset for(int i=0; i