forked from mayankb110/HacktoberFest22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnapsackExample.java
34 lines (34 loc) · 844 Bytes
/
KnapsackExample.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
public class KnapsackExample
{
public int max(int a1, int a2)
{
return (a1 > a2) ? a1 : a2;
}
public int maxknapSackVal(int C, int wt[], int v[], int l)
{
if (l == 0 || C == 0)
{
return 0;
}
if (wt[l - 1] > C)
{
return maxknapSackVal(C, wt, v, l - 1);
}
else
{
int val1 = maxknapSackVal(C - wt[l - 1], wt, v, l - 1);
int val2 = maxknapSackVal(C, wt, v, l - 1);
return max(v[l - 1] + val1, val2);
}
}
public static void main(String argvs[])
{
int values[] = new int[] { 100, 60, 120 };
int weight[] = new int[] { 20, 10, 30 };
int C = 50;
int length = values.length;
KnapsackExample knapObj = new KnapsackExample();
int maxVal = knapObj.maxknapSackVal(C, weight, values, length);
System.out.println("The maximum value is: " + maxVal);
}
}