**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 :

` ````
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

## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →