# Check if the game is valid or not

Three players P1, P2 and P3 are playing a game. But at a time only two players can play the game, so they decided, at a time two players will play the game and one will spectate. When one game ends, the one who lost the game becomes the spectator in the next game and the one who was spectating plays against the winner. There cannot be any draws in any game. Player P1 and P2 will play the first game. Input is the number of games played n and in next line winner of n games.

Examples :

```Input :
No. of Games : 4
Winner of the Game Gi : 1 1 2 3
Output : YES
Explanation :
Game1 : P1 vs P2 : P1 wins
Game2 : P1 vs P3 : P1 wins
Game3 : P1 vs P2 : P2 wins
Game4 : P3 vs P2 : P3 wins
None of the winners were invalid

Input :
No. of Games : 2
Winner of the Game Gi : 2 1
Output : NO
Explanation :
Game1 : P1 vs P2 : P2 wins
Game2 : P2 vs P3 : P1 wins (Invalid winner)
In Game2 P1 is spectator
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :
Start with P1 and P2 and continue the game while making the loser new spectator by subtracting the current spectator and winner from total sum of three players i.e. 6. An invalid entry will halt the process.

Below is the implementation for the above approach:

## C++

 `// CPP program to check if the game ` `// is valid ` `#include ` `using` `namespace` `std; ` ` `  `string check_valid(``int` `a[], ``int` `n) ` `{ ` `    ``// starting with player P1 and P2  ` `    ``// so making P3 spectator ` `    ``int` `spec = 3; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `     `  `        ``// If spectator wins a game  ` `        ``// then its not valid ` `        ``if` `(a[i] == spec) { ` `            ``return` `"Invalid"``; ` `        ``} ` `         `  `        ``// subtracting the current spectator  ` `        ``// and winner from total sum 6 which ` `        ``// makes losing player spectator ` `        ``spec = 6 - a[i] - spec; ` `    ``} ` `     `  `    ``// None of the winner is found spectator. ` `    ``return` `"Valid"``; ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` `    ``int` `n = 4; ` `    ``int` `a[n] = {1, 1, 2, 3}; ` `    ``cout << check_valid(a, n); ` `    ``return` `0; ` `} `

## Java

 `// java program to check if the game ` `// is valid ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  ` `  `     `  `    ``static` `String check_valid(``int` `a[], ``int` `n) ` `    ``{ ` `        ``// starting with player P1 and P2  ` `        ``// so making P3 spectator ` `        ``int` `spec = ``3``; ` `         `  `        ``for` `(``int` `i = ``0``; i < n; i++) { ` `         `  `            ``// If spectator wins a game  ` `            ``// then its not valid ` `            ``if` `(a[i] == spec) { ` `                ``return` `"Invalid"``; ` `            ``} ` `             `  `            ``// subtracting the current spectator  ` `            ``// and winner from total sum 6 which ` `            ``// makes losing player spectator ` `            ``spec = ``6` `- a[i] - spec; ` `        ``} ` `         `  `        ``// None of the winner is found spectator. ` `        ``return` `"Valid"``; ` `    ``} ` `     `  `    ``// Driver program to test above functions ` `    ``public` `static` `void` `main (String[] args) { ` `         `  `        ``int` `a[] = {``1``, ``1``, ``2``, ``3``}; ` `        ``int` `n = a.length; ` `         `  `        ``System.out.println(check_valid(a, n)); ` `     `  `        ``} ` `} ` ` `  `// This code is contributed by vt_m `

## Python3

 `# Python3 code to check if the game ` `# is valid ` ` `  `def` `check_valid( a , n ): ` ` `  `    ``# starting with player P1 and P2 ` `    ``# so making P3 spectator ` `    ``spec ``=` `3` `    ``for` `i ``in` `range``(n): ` `         `  `        ``# If spectator wins a game ` `        ``# then its not valid ` `        ``if` `a[i] ``=``=` `spec: ` `            ``return` `"Invalid"` `         `  `        ``# subtracting the current spectator ` `        ``# and winner from total sum 6 which ` `        ``# makes losing player spectator ` `        ``spec ``=` `6` `-` `a[i] ``-` `spec ` `     `  `    ``# None of the winner is found spectator. ` `    ``return` `"Valid"` ` `  `# Driver code to test above functions ` `n ``=` `4` `a ``=` `[``1``, ``1``, ``2``, ``3``] ` `print``(check_valid(a, n)) ` ` `  `# This code is contributed by "Sharad_Bhardwaj". `

## C#

 `// C# program to check if the game ` `// is valid ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``static` `String check_valid(``int` `[]a, ``int` `n) ` `    ``{ ` `        ``// starting with player P1 and P2  ` `        ``// so making P3 spectator ` `        ``int` `spec = 3; ` `         `  `        ``for` `(``int` `i = 0; i < n; i++) { ` `         `  `            ``// If spectator wins a game  ` `            ``// then its not valid ` `            ``if` `(a[i] == spec) { ` `                ``return` `"Invalid"``; ` `            ``} ` `             `  `            ``// subtracting the current spectator  ` `            ``// and winner from total sum 6 which ` `            ``// makes losing player spectator ` `            ``spec = 6 - a[i] - spec; ` `        ``} ` `         `  `        ``// None of the winner is found spectator. ` `        ``return` `"Valid"``; ` `    ``} ` `     `  `    ``// Driver program  ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `[]a = {1, 1, 2, 3}; ` `        ``int` `n = a.Length; ` `         `  `        ``Console.WriteLine(check_valid(a, n)); ` `     `  `        ``} ` `} ` ` `  `// This code is contributed by vt_m `

## PHP

 ` `

Output :

```Valid
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.