-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXORPAL1.java
More file actions
58 lines (46 loc) · 1.46 KB
/
Copy pathXORPAL1.java
File metadata and controls
58 lines (46 loc) · 1.46 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.Scanner;
public class XORPAL1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Test cases
short T = scanner.nextShort();
for (short i = 1; i <= T; i++) {
// Input
int N = scanner.nextInt();
scanner.nextLine();
String S = scanner.nextLine();
// Logic
if (N % 2 != 0) {
// Case 1: Length == odd always possible to arrange
System.out.println("YES");
} else {
// Case 2: To be arranged condition and even length
int result = -1;
int noZero = 0;
int noOne = 0;
for (int j = 0; j < N; j++) {
if (S.charAt(j) == '0') {
noZero++;
} else {
noOne++;
}
}
if (noOne == noZero) {
result = 1;
}
if (noOne == N || noZero == N) {
result = 1;
}
if (noOne % 2== 0 && noZero % 2 == 0) {
result = 1;
}
// Output
if (result == -1) {
System.out.println("NO");
} else {
System.out.println("YES");
}
}
}
}
}