Two participants, A and B, are playing the following game:
- The initial set of games is a sequence of zeroes and ones.
- A and B change turns, and participant A always moves first.
- During each turn, a player removes one element from the sequence that satisfies the following: It is not the first or last element. It must be surrounded by zeroes on both sides.
- The first participant who can't take their turn loses the game.
- Both participants always move optimally.
Input Format
The first line contains an integer n, denoting the number of games. The subsequent lines describe each game in the following format:
- The first line before the sequence contains a single integer denoting the length of the sequence.
- The second line contains the sequence, i.e. a space-separated integers denoting the respective values of the initial sequence. Each element is either zero or one.
Sample input
8
61
1 0 1 1 0 1 0 1 1 0 1 1 0 0 1 0 1 0 0 1 0 1 0 1 0 0 0 1 1 0 1 0 1 1 0 1 0 1 0 0 1 1 0 0 1 1 0 0 1 0 1 0 1 0 1 0 1 1 1 0 1
28
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
28
1 0 1 1 0 1 0 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 0 1 0 1 1
15
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
5
1 0 1 0 1
6
1 0 1 0 1 1
6
0 0 0 0 0 0
7
1 0 1 0 1 0 0
Sample Output
A
B
B
A
A
A
B
B
Graphical explanation for 5th and 7th sample inputs:
Number to be removed during each turn is marked in red.
My code:
public class Solution {
public enum Players {
A { @Override public Players turn() {return B; }},
B { @Override public Players turn() {return A; }},
;
abstract public Players turn();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int g = in.nextInt();
Players winner = Players.B;
for(int a0 = 0; a0 < g; a0++){
int n = in.nextInt();
Integer[] sequence = new Integer[n];
for(int sequence_i=0; sequence_i < n; sequence_i++){
sequence[sequence_i] = in.nextInt();
}
ArrayList<Integer> initial = new ArrayList<Integer>(Arrays.asList(sequence));
Players current = Players.B;
for (int i=0; i < initial.size()-1; i++) {
if (i > 0 && initial.get(i-1) == 0 && initial.get(i+1) == 0) {
current = current.turn();
winner = current;
initial.remove(i);
i = 0;
}
}
System.out.println(winner);
}
in.close();
}
}
Is my code logically correct? The test data is hit but I doubt anyway...


The test data is hit, so on my test cases the code is working. But I may have missed some cases out of my sight... \$\endgroup\$nwhich is number of games, and then there go line tuples: sequence length + sequence itself on every two lines. Maybe I worded it awkward, dunno how to formulate better, 'cause English is non-native for me. \$\endgroup\$