-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnapsack10.java
44 lines (39 loc) · 1023 Bytes
/
Knapsack10.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
package Unit_3;
import java.util.*;
public class Knapsack10 {
static int knapsack(int W, int wt[], int val[], int n)
{
int i, w;
int k[][] = new int[n + 1][W + 1];
for(i = 0; i <= n; i++)
{
for(w = 0; w <= W; w++)
{
if(i == 0 || w == 0)
k[i][w] = 0;
else if(wt[i - 1] <= w)
k[i][w] = Math.max(val[i - 1] + k[i - 1][w - wt[i - 1]], k[i - 1][w]);
else
k[i][w] = k[i - 1][w];
}
}
return k[n][W];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of items : ");
int n = sc.nextInt();
System.out.print("Enter weight of items : ");
int wt[] = new int[n];
for(int i = 0; i < n; i++)
wt[i] = sc.nextInt();
System.out.print("Enter item value : ");
int val[] = new int[n];
for(int i = 0; i < n; i++)
val[i] = sc.nextInt();
System.out.print("Enter maximum capacity of Knapsack : ");
int W = sc.nextInt();
System.out.println("Max Profit = " + knapsack(W, wt, val, n));
sc.close();
}
}