How to find remaining scores from score list after a player is ahead by two

64 views Asked by At

Basically need to find a list of integers, representing the sequence of points remaining in the match after the current game; this should take the form of the remainder of the points input after the points for the current (complete or incomplete) game have been removed. The points are simply in a 1 or 0 fashion in a list (where 1 is scoring the point). Player wins if ahead by 2 points and the rest of the remaining scores from list need to be separated. So for example if I have a list [1,0,1,0,1,1,1,0,1,1,1] The player has score 8 times. But since the player by the 5th point they score they are 2 above his/her opponent and thus wins the game, but I need to find the remains scores which should be [0,1,1,1]

I am able to distinguish the scores but I am not sure how to find the point where the winner arises. Any help or guidance would be appreciated.

1

There are 1 answers

3
muzzlator On

Add 1 to some count when you see a 1, add -1 when you see a 0. Once the count reaches 2 or -2, you can stop and return the remaining part of the array

int count = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] == 1) {
        count++;
    } else {
        count--;
    }
    if (count == 2 || count == -2) {
        System.out.println("The game ended on round " + i);
        break; // This command just exits the for loop.
    }
}