Game on numeric arrays 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 &lt; g; a0++){
        int n = in.nextInt();
        Integer[] sequence = new Integer[n];
        for(int sequence_i=0; sequence_i &lt; n; sequence_i++){
            sequence[sequence_i] = in.nextInt();
        }
        ArrayList&lt;Integer&gt; initial = new ArrayList&lt;Integer&gt;(Arrays.asList(sequence));
        Players current = Players.B;
        for (int i=0; i &lt; initial.size()-1; i++) {  
            if (i &gt; 0 &amp;&amp; initial.get(i-1) == 0 &amp;&amp; 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...
for (int i=0; i &lt; initial.size()-1; i++) {  
            if (i &gt; 0 &amp;&amp; initial.get(i-1) == 0 &amp;&amp; initial.get(i+1) == 0) {
                current = current.turn();
                winner = current;
                initial.remove(i);
                i = 0;
            }
        }



You don't need to do initial.size() - 1 on each iteration.  Consider 

        for (int i = 2; i &lt; initial.size(); i++) {  
            if (initial.get(i) == 0 &amp;&amp; initial.get(i - 2) == 0) {
                current = current.turn();
                initial.remove(i - 1);
                i = 1;
            }
        }


This starts with the third element.  If it's not a 0, then there is no reason to check other values.  No math needed.  If it is a 0, then it verifies that the element two places prior is a 0.  Only then does it proceed to processing.  

Note that setting i to 1 means that it will be 2 again for the next iteration.  There were actually offsetting mistakes in the original code that made it work.  You don't need i to ever be 0, so setting i to 0 and then incrementing to 1 worked.