# 21 Matchsticks Problem

Given 21 Matchsticks and 2 users, A and B (computer and user respectively). Users can pick matchsticks not more than four at a time. The one who is forced to pick the last matchstick loses.

Given an array arr[] which contains the moves of the computer. The task is to print the moves of the user so that the user wins the game.

Examples:

Input : N = 4, arr=[ 3, 4, 2, 2]
Output : 2, 1, 3, 3
When the computer chooses 3 sticks, the user chooses 2 sticks
When the computer chooses 4 sticks, the user chooses 1 stick
When the computer chooses 2 sticks, the user chooses 3 sticks
When the computer chooses 2 sticks, the user chooses 3 sticks
Now only 1 stick is left and the computer is forced to pick that stick
Hence the user wins the game.

Input : N = 4 arr=[ 1, 1, 4, 3]
Output : 4, 4, 1, 2

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

Approach:

• Idea is to think for 20 matchsticks as the user who would pick the last one will lose the game.
• Divide 20 into four parts that is, each part is of size 5. So if the computer picks x matchsticks then the user should pick (5-x) matchsticks and should proceed in the same way.
• In this way, 20 matchsticks will be used and the last matchstick would be picked by the computer.

Below is the implementation of the above approach

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the optimal strategy ` `void` `TwentyoneMatchstick(``int` `arr[], ``int` `N) ` `{ ` ` `  `    ``// Removing matchsticks in blocks of five ` `    ``for` `(``int` `i = 0; i < N; i += 1) { ` `        ``cout << 5 - arr[i] << ``" "``; ` `    ``} ` `    ``cout << endl; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 3, 4, 2, 2 }; ` ` `  `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``TwentyoneMatchstick(arr, N); ` ` `  `    ``return` `0; ` `} `

 `// Java implementation of the approach ` `class` `GFG  ` `{ ` ` `  `// Function to return the optimal strategy ` `static` `void` `TwentyoneMatchstick(``int` `arr[], ``int` `N) ` `{ ` ` `  `    ``// Removing matchsticks in blocks of five ` `    ``for` `(``int` `i = ``0``; i < N; i += ``1``)  ` `    ``{ ` `        ``System.out.print(``5` `- arr[i] + ``" "``); ` `    ``} ` `    ``System.out.println(); ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `arr[] = {``3``, ``4``, ``2``, ``2``}; ` ` `  `    ``int` `N = arr.length; ` ` `  `    ``TwentyoneMatchstick(arr, N); ` `} ` `}  ` ` `  `// This code is contributed by Princi Singh `

 `# Python3 implementation of the approach ` ` `  `# Function to return the optimal strategy ` `def` `TwentyoneMatchstick(arr, N): ` ` `  `    ``# Removing matchsticks in blocks of five ` `    ``for` `i ``in` `range``(N): ` `        ``print``(``5` `-` `arr[i], end ``=` `" "``) ` ` `  `# Driver code ` `arr ``=` `[``3``, ``4``, ``2``, ``2` `] ` ` `  `N ``=` `len``(arr) ` ` `  `TwentyoneMatchstick(arr, N) ` ` `  `# This code is contributed  ` `# by Mohit Kumar `

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to return the optimal strategy ` `static` `void` `TwentyoneMatchstick(``int` `[]arr, ``int` `N) ` `{ ` ` `  `    ``// Removing matchsticks in blocks of five ` `    ``for` `(``int` `i = 0; i < N; i += 1)  ` `    ``{ ` `        ``Console.Write(5 - arr[i] + ``" "``); ` `    ``} ` `    ``Console.Write(``"\n"``); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``int` `[]arr = {3, 4, 2, 2}; ` ` `  `    ``int` `N = arr.Length; ` ` `  `    ``TwentyoneMatchstick(arr, N); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:
```2 1 3 3
```

Time Complexity: O(N)

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.

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 : mohit kumar 29, princi singh

Article Tags :
Practice Tags :