-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoinChangeMaxNumber.java
51 lines (44 loc) · 1.18 KB
/
CoinChangeMaxNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// To find maximum number of ways for coin change.
//Solution.
public class CoinChangeMaxNumber{
public static void main(String []args){
int coin[] = {1,3,2};
int total = 10;
int len = coin.length;
int i,j;
int[][] arr = new int[len+1][total+1];
for(i = 0;i<=len;i++)
{
for (j =0;j<=total;j++)
{
if (i==0)
arr[i][j] = 0;
if (j ==0)
arr[i][j] =1;
}
}
for(i=1;i<=len;i++)
{
for (j =1;j<=total;j++)
{
if(coin[i-1]>j)
{
arr[i][j] = arr[i-1][j];
}
else
{
arr[i][j] = arr[i-1][j] + arr[i][j-(coin[i-1])];
}
}
}
for(i = 0;i<=len;i++)
{
for (j =0;j<=total;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}
System.out.println("Maximum Number of total ways are : " + arr[len][total]);
}
}