-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpartition.java
More file actions
executable file
·49 lines (43 loc) · 1014 Bytes
/
Copy pathpartition.java
File metadata and controls
executable file
·49 lines (43 loc) · 1014 Bytes
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
import java.util.*;
class Demo {
static int MAX = 1024;
static boolean [][] possible = new boolean[MAX][MAX];
static int [] a = {3, 2, 1, 6};
public static void main(String [] args) {
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += i;
}
if (sum % 2 != 0) {
System.out.println("Impossible");
} else {
possible[a[0]][0] = true;
possible[0][0] = true;
for (int j = 1; j < a.length; j++) {
for (int i = 0; i <= sum; i++) {
if (possible[i][j-1]) {
possible[i][j] = true;
}
int previous = i - a[j];
if (previous >= 0) {
if (possible[previous][j-1]) {
possible[i][j] = true;
}
}
}
}
System.out.print("\t");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println();
for (int i = 0; i <= sum; i++) {
System.out.print(i + "\t");
for (int j = 0; j < a.length; j++) {
System.out.print(possible[i][j] + "\t");
}
System.out.println();
}
}
}
}