-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZOKnapsack.java
More file actions
59 lines (51 loc) · 1.95 KB
/
ZOKnapsack.java
File metadata and controls
59 lines (51 loc) · 1.95 KB
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
52
53
54
55
56
57
58
59
import java.util.*;
import java.io.*;
class ZOKnapsack{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintStream ps = new PrintStream(System.out);
//taking inputs of no. of items
ps.println("Enter the no. of items and maximum weight!");
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int max = Integer.parseInt(st.nextToken());
//declaring weight and profit array
int weight[] = new int[n];
int profit[] = new int[n];
ps.println("Enter the elements of the weight array!");
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++){
weight[i] = Integer.parseInt(st.nextToken());
}
ps.println("Enter the elements of the values array!");
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++){
profit[i] = Integer.parseInt(st.nextToken());
}
//Declaring knapsack array
int knapsack[][] = new int[n+1][max+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=max;j++){
if(i==0 || j==0)
knapsack[i][j] = 0;
else if(j>=weight[i-1])
knapsack[i][j] = Math.max(knapsack[i-1][j], knapsack[i-1][j-weight[i-1]]+profit[i-1]);
else
knapsack[i][j] = knapsack[i-1][j];
}
}
System.out.println("Maximum profit will be "+ knapsack[n][max]);
int j=max;
int v = knapsack[n][j];
//printing profit of the elements which will be taken
for(int i=n;i>0 && v>0;i--){
if(knapsack[i-1][j]==v)
continue;
else{
ps.print(profit[i-1]+" ");
v=v-profit[i-1];
j=j-weight[i-1];
}
}ps.println();
}
}