-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathisCentered15.java
59 lines (47 loc) · 1.21 KB
/
isCentered15.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package mumTestPackage;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class MumTest18 {
@Test
void test() {
assertEquals(1, isCentered15(new int[] {3, 2, 10, 15, 1, 6, 9}));
assertEquals(0, isCentered15(new int[] {2, 10, 4, 1, 6, 9}));
assertEquals(0, isCentered15(new int[] {3, 2, 10, 4, 1, 6}));
assertEquals(1, isCentered15(new int[] {1,1,8, 3, 1, 1}));
assertEquals(1, isCentered15(new int[] {9, 15, 6}));
assertEquals(0, isCentered15(new int[] {1, 1, 2, 2, 1, 1}));
assertEquals(1, isCentered15(new int[] {3, 12, 15, 2, 4}));
}
int isCentered15(int[ ] a) {
if(a == null || a.length == 0)
return 0;
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum = a[i];
if(sum == 15 && isCenterArray(i+1, i+1, a.length))
return 1;
if (sum > 15) {
continue;
}
for (int j = i+1; j < a.length; j++) {
sum +=a[j];
if(sum == 15 && isCenterArray(i+1, j+1, a.length))
return 1;
if (sum > 15) {
break;
}
}
}
return 0;
}
boolean isCenterArray(int start, int end, int length) {
int left = start-1;
int right = length - end;
if(left == right) {
return true;
}
else {
return false;
}
}
}