class Solution { /** Recursion */ public int count(int coins[], int sum) { int n = coins.length; return countCombinations(coins, sum, n); } public int countCombinations(int[] coins, int sum, int n) { // base condition: when sum becomes 0 then we found valid combination if(sum == 0) return 1; // base condition: when we reach end of array, no valid combination found if(n == 0) return 0; if(coins[n-1] <= sum) { // include the current coin into combination, and this coin can be used later too int pick = countCombinations(coins, sum - coins[n-1], n); // do not include the current coin into combination int notPick = countCombinations(coins, sum, n-1); return pick + notPick; } // when current coin has value greater than the required sum, skip the coin return countCombinations(coins, sum, n-1); } /** Memoization */ public int count(int coins[], int sum) { int n = coins.length; int[][] t = new int[n+1][sum+1]; // when array size keeps on increasing but sum is 0, so we can have empty list for(int i=0; i