21 Matchsticks Problem - GeeksforGeeks

26 Apr, 2021
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  
 
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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the optimal strategyvoid 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 codeint main(){    int arr[] = { 3, 4, 2, 2 };     int N = sizeof(arr) / sizeof(arr[0]);     TwentyoneMatchstick(arr, N);     return 0;}
// Java implementation of the approachclass GFG{ // Function to return the optimal strategystatic 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 codepublic 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 strategydef TwentyoneMatchstick(arr, N):     # Removing matchsticks in blocks of five    for i in range(N):        print(5 - arr[i], end = " ") # Driver codearr = [3, 4, 2, 2 ] N = len(arr) TwentyoneMatchstick(arr, N) # This code is contributed# by Mohit Kumar
// C# implementation of the approachusing System; class GFG{ // Function to return the optimal strategystatic 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 codepublic 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
// javascript implementation of the approach   // Function to return the optimal strategy function TwentyoneMatchstick(arr, N){       // Removing matchsticks in blocks of five    for (var i = 0; i < N; i += 1)    {        document.write(5 - arr[i] + " ");    }    document.write("<br>");}   // Driver code    var arr = [3, 4, 2, 2];    var N = arr.length;    TwentyoneMatchstick(arr, N); // This code is contributed by bunnyram19.  
2 1 3 3
 
Time Complexity: O(N) 
mohit kumar 29
princi singh
bunnyram19
Constructive Algorithms
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Introduction to Arrays
Linear Search
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers