Open In App

Game of stones

Given an integer N which is the number of stones in a pile and the game of stones is being played between you and your friend, the task is to find out whether you’ll win or not. A player (on his/her turn) can remove 1 2 or 3 stones, the game continues in alternating turns. The one who is not able to make the move loses the game. In other words, the player who removes the last set of stones always wins. Print YES if you can win the game else print NO

Examples: 



Input: N = 4 
Output: NO 
Explanation: If there are 4 stones in the bag, then you will never win the game. No matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend. Hence, you are not able to make the move.

Input: N = 3 
Output: YES 
Explanation: Yes you can win the game, you just remove all 3 stones in your turn. 



Approach: 

Below is the implementation of the above approach: 




// C++ program of game of stones
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns true if u win
bool checkWin(int n)
{
    if (n % 4 != 0)
        return true;
    return false;
}
 
// Driver code
int main()
{
    // n is number of stones
    int n = 4;
 
    if (checkWin(n))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}




// Java program of game of stones
import java.io.*;
 
class GFG {
    // Function that returns true if u win
static boolean checkWin(int n)
{
    if (n % 4 != 0)
        return true;
    return false;
}
 
// Driver code
    public static void main (String[] args) {
     
    // n is number of stones
    int n = 4;
 
    if (checkWin(n))
            System.out.println("YES");
    else
            System.out.println( "NO");
         
    }
//This code is contributed by akt_mit   
}




# Python3 program of game of stones
  
# Function that returns true if u win
def checkWin( n):
 
    if (n % 4 != 0):
        return True
    return False
  
# Driver code
if __name__ == "__main__":
 
    # n is number of stones
    n = 4
  
    if (checkWin(n)):
        print ( "YES")
    else:
        print ("NO")




//C#  program of game of stones
using System;
 
public class GFG{
// Function that returns true if u win
static bool checkWin(int n)
{
    if (n % 4 != 0)
        return true;
    return false;
}
 
// Driver code
     
    static public void Main (){
         
    // n is number of stones
    int n = 4;
 
    if (checkWin(n))
            Console.WriteLine("YES");
    else
            Console.WriteLine( "NO");
         
    }
//This code is contributed by ajit
}




<script>
    // Javascript program of game of stones
     
    // Function that returns true if u win
    function checkWin(n)
    {
        if (n % 4 != 0)
            return true;
        return false;
    }
     
    // n is number of stones
    let n = 4;
   
    if (checkWin(n))
          document.write("YES");
    else
          document.write( "NO");
         
</script>




<?php
// PHP program of game of stones
 
// Function that returns
// true if u win
function checkWin($n)
{
    if ($n % 4 != 0)
        return true;
    return false;
}
 
// Driver code
 
// n is number of stones
$n = 4;
 
if (checkWin($n))
    echo "YES","\n" ;
else
    echo "NO","\n" ;
 
// This code is contributed
// by ANKITRAI1
?>

Output
NO


Time Complexity: O(1) 
Auxiliary Space: O(1)


Article Tags :