-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualSplit.java
More file actions
35 lines (28 loc) · 852 Bytes
/
EqualSplit.java
File metadata and controls
35 lines (28 loc) · 852 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
public class EqualSplit {
public static void main(String[] args) {
int[] vals = {12, 7, 3, 11, 8, 5, 9, 10, 6, 3, 8, 2, 1, 7, 8, 1, 11, 4, 5, 12, 6, 2, 9, 0, 10, 4, 7, 11, 5, 3, 8, 0, 6};
int totalSum = 0;
for (int val : vals) {
totalSum += val;
}
if (totalSum % 2 != 0) {
System.out.println("Cannot split array into equal sum halves.");
return;
}
int halfSum = totalSum / 2;
int sum = 0;
boolean canSplit = false;
for (int val : vals) {
sum += val;
if (sum == halfSum) {
canSplit = true;
break;
}
}
if (canSplit) {
System.out.println("good");
} else {
System.out.println("cannot split");
}
}
}