-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanced.java
More file actions
31 lines (27 loc) · 938 Bytes
/
Copy pathBalanced.java
File metadata and controls
31 lines (27 loc) · 938 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
public class Balanced {
public static void main(String[] args) {
System.out.println(isBalanced(new int[]{-2, 3, 2, -3}));
System.out.println(isBalanced(new int[]{-2, 2, 2, 2}));
System.out.println(isBalanced(new int[]{-5, 2, -2}));
System.out.println();
System.out.println(isBalanced(new int[]{2, 3, 6, 7}));
System.out.println(isBalanced(new int[]{6, 7, -2, 3, -12}));
System.out.println(isBalanced(new int[]{7, 15, 2, 3}));
System.out.println(isBalanced(new int[]{-6, 6, 6}));
}
public static int isBalanced(int[] a) {
boolean flagBalanced = false;
for(int i = 0; i < a.length; i++) {
flagBalanced = false;
for(int j = 0; j < a.length; j++) {
if(a[i] + a[j] == 0) {
flagBalanced = true;
break;
}
}
if(!flagBalanced)
return 0;
}
return 1;
}
}