Explain the approach applied in the solution

Please explain this solution for the problem.

In this problem, basically there are three players. one player is watching other two players play. So, it is natural that the third person, who is watching the game, cannot win.

We are given a sequence consisting of N integers stating that ith game is won by A[i].

We just have to check that at no point should the spectator win.

The solution You mentioned create a boolean array of size 3. if player[i] = true, ith player is playing right now.

Initially first two players are playing. So play[0] = true and play[1] = true

Then, in loop, he check if the play[A[i]-1] == false. This line means that the winner as per sequence is playing or not.

if play[A[i]-1] == false, sequence is not valid.

otherwise he changed status of all playing players to false, and the third player, whose play[i] == false, set play[i] = true

Then, as the problem said, winner play the next round, he set play[a[i]-1] = true

And this continues till N. and print answer.

2 Likes

Thank you.

No Problem mate.