0

error in this code?

package DP1; public class CoinSumprob { public static void main(String[] args) { // TODO Auto-generated method stub int coins[] = { 1, 2, 3 }; int n = coins.length; int dp[][]=new int [n+1][4+1]; for(int i=0;i<dp.length;i++) { for(int j=0;j<dp.length;j++) { dp[i][j]=-1; } } System.out.println(count(coins, n, 4,dp)); } static int count(int coins[], int n, int sum,int dp[][]) { // If sum is 0 then there is 1 solution // (do not include any coin) if (sum == 0) return 1; // If sum is less than 0 then no // solution exists if (sum < 0) return 0; // If there are no coins and sum // is greater than 0, then no // solution exist if (n <= 0) return 0; // count is sum of solutions (i) // including coins[n-1] (ii) excluding coins[n-1] // return count(coins, n - 1, sum) // + count(coins, n, sum - coins[n - 1]); int small1,small2; if(dp[n-1][sum]==-1) { small1=count(coins, n-1, sum, dp); dp[n-1][sum]=small1; } else { small1=dp[n-1][sum]; } if(dp[n-1][sum-coins[n-1]]==-1) { small2=count(coins, n-1, sum-coins[n-1], dp); dp[n-1][sum-coins[n-1]]=small2; } else { small2=dp[n-1][sum-n-1]; } return small1+small2; } }

22nd Jul 2023, 5:02 PM
subrat yadav
2 Answers
+ 1
Abdi Jawate don't attach of someone else code in Q&A discussion forum
23rd Jul 2023, 4:42 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar