Two participants, A and B, are playing the following game:The initial set of games is a sequence of zeroes and ones. A and Bchange turns, and participant A always moves first. During eachturn, a player removes one element from the sequence that satisfiesthe following: It is not the first or last element. It must besurrounded by zeroes on both sides. The first participant who can't taketheir turn loses the game. Both participants always move optimally.Input FormatThe 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 input8611 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 1280 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 0281 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 1150 1 0 1 0 1 0 1 0 1 0 1 0 1 051 0 1 0 161 0 1 0 1 160 0 0 0 0 071 0 1 0 1 0 0Sample OutputABBAAABBGraphical 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...
Game on numeric arrays
java;array
        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;            }        }You don't need to do initial.size() - 1 on each iteration.  Consider         for (int i = 2; i < initial.size(); i++) {              if (initial.get(i) == 0 && 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.  