Game of Stones


Problem Statement :


Two players called  and  are playing a game with a starting number of stones. Player  always plays first, and the two players move in alternating turns. The game's rules are as follows:

In a single move, a player can remove either , , or  stones from the game board.
If a player is unable to make a move, that player loses the game.
Given the starting number of stones, find and print the name of the winner.  is named First and  is named Second. Each player plays optimally, meaning they will not make a move that causes them to lose the game if a winning move exists.

For example, if ,  can make the following moves:

 removes  stones leaving .  will then remove  stones and win.
 removes  stones leaving .  cannot move and loses.
 would make the second play and win the game.


Function Description

Complete the gameOfStones function in the editor below. It should return a string, either First or Second.

gameOfStones has the following parameter(s):

n: an integer that represents the starting number of stones

Input Format

The first line contains an integer , the number of test cases.
Each of the next  lines contains an integer , the number of stones in a test case.


Output Format

On a new line for each test case, print First if the first player is the winner. Otherwise print Second.



Solution :



title-img


                            Solution in C :

In   C  :




#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

   
    int t;
    scanf("%d",&t);
    while(t--)
        {
        int n;
        scanf("%d",&n);
        int dp[n+1];
        dp[0]=dp[1]=1;
        dp[4]=dp[2]=dp[3]=dp[5]=0;
        int i;
        for(i=6;i<=n;i++)
            {
            if(dp[i-5]||dp[i-2]||dp[i-3])
                dp[i]=0;
            else
                dp[i]=1;
        }
        if(dp[n])
            printf("Second\n");
        else
            printf("First\n");
    }
    return 0;
}
                        


                        Solution in C++ :

In  C++  :





#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
long DP[101];
bool dp(long x)
{
	if (DP[x]!=-1) return DP[x];
	bool res=!dp(x-2);
	if (x>3) res=res|(!dp(x-3));
	if (x>5) res=res|(!dp(x-5));
	DP[x]=res;
	return res;
}
int main()
{
	long nTest,n;
	memset(DP,-1,sizeof(DP));
	DP[1]=0;
	DP[2]=DP[3]=DP[5]=1;
	scanf("%ld",&nTest);
	while (nTest--)
	{
		scanf("%ld",&n);
		puts(dp(n)?"First":"Second");
	}
}
                    


                        Solution in Java :

In  Java :






import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int no_of_testcases=Integer.parseInt(br.readLine());
        while(no_of_testcases-->0){
            int number=Integer.parseInt(br.readLine());
            if(number%7==0 || number%7==1){
                System.out.println("Second");
            }
            else{
                System.out.println("First");
            }
        }
    }
}
                    


                        Solution in Python : 
                            
In   Python3 :






def get_winner(n):
    ans=[None]*(n+1)
    ans[0]=False
    ans[1]=False
    for i in range(n+1):
        if ans[i]==False:
            for j in [i+2,i+3,i+5]:
                if j<n+1:
                    ans[j]=True
        elif ans[i] is None:
            for j in [i-2, i-3, i-5]:
                if j>0 and ans[j]!=True:
                    print('??',i,j,ans[j])
            ans[i]=False
            for j in [i+2,i+3,i+5]:
                if j<n+1:
                    ans[j]=True
        # print(i,ans)
    return ans

w=get_winner(100)
for _ in range(int(input().strip())):
    print('First' if w[int(input().strip())] else 'Second')
                    


View More Similar Problems

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →