-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroBalanced.java
More file actions
44 lines (38 loc) · 1.34 KB
/
Copy pathZeroBalanced.java
File metadata and controls
44 lines (38 loc) · 1.34 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
public class ZeroBalanced {
public static void main(String[] args) {
System.out.println(isZeroBalanced(new int[]{1, 2, -2, -1}));
System.out.println(isZeroBalanced(new int[]{-3, 1, 2, -2, -1, 3}));
System.out.println(isZeroBalanced(new int[]{3, 4, -2, -3, -2}));
System.out.println(isZeroBalanced(new int[]{0, 0, 0, 0, 0, 0}));
System.out.println(isZeroBalanced(new int[]{-3, -3, -3}));
System.out.println(isZeroBalanced(new int[]{3}));
System.out.println(isZeroBalanced(new int[]{}));
System.out.println(isZeroBalanced(new int[]{3, -1, -2, -3, 3}));
System.out.println(isZeroBalanced(new int[]{4, 1, -4, -1}));
}
static int isZeroBalanced(int[] a) {
if (a.length <= 1)
return 0;
int sum = 0;
boolean negativeFlag = false;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
if (sum != 0)
return 0;
for (int j = 0; j < a.length; j++) {
if (a[j] > 0) {
negativeFlag = false;
for (int k = 0; k < a.length; k++) {
if (a[j] == -a[k]) {
a[j] = a[k] = 0;
negativeFlag = true;
break;
}
}
if (!negativeFlag) return 0;
}
}
return 1;
}
}