-
Notifications
You must be signed in to change notification settings - Fork 7
/
NumarrEquals.java
35 lines (32 loc) · 941 Bytes
/
NumarrEquals.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 Main {
public static void main(String[] args) {
Integer[] numarr = new Integer[] { 7, 4, 2, 9 };
Integer sum = 0;
for (Integer number : numarr) {
sum += number;
}
if (sum % 2 == 1) {
System.out.println("NO");
return;
}
Integer halfSum = sum / 2;
System.out.println(numarrEquals(numarr, 0, halfSum));
}
private static Boolean numarrEquals(Integer[] numarr, Integer index, Integer value) {
if (numarr[index] == value) {
return true;
}
if (index == numarr.length - 1) {
return false;
}
Integer valueIndex = numarr[index];
index++;
if (numarrEquals(numarr, index, value - valueIndex)) {
return true;
}
if (numarrEquals(numarr, index, value)) {
return true;
}
return false;
}
}