Given an integer N denoting the number of boxes in a pen, and two players P1 and P2 playing a game of distributing N pens among themselves as per the following rules:
- P1 makes the first move by taking 2X pens. (Initially, X = 0)
- P2 takes 3X pens.
- Value of X increases by 1 after each move.
- P1 and P2 makes move alternatively.
- If the current player has to take more pens than the number of pens remaining in the box, then they quit.
- The game will be over when both the players quit or when the box becomes empty.
The task to print the following details once the game is over:
- The number of pens remaining in the box.
- The number of pens collected by P1.
- The number of pens collected by P2.
Examples:
Input: N = 22
Output:
Number of pens remaining in the box: 14
Number of pens collected by P1 : 5
Number of pens collected by P2 : 3
Explanation:
- Move 1: X = 0, P1 takes 1 pen from the box. Therefore, N = 22 – 1 = 21.
- Move 2: X = 1, P2 takes 3 pens from the box. Therefore, N = 21 – 3 = 18.
- Move 3: X = 2, P1 takes 4 pens from the box. Therefore, N = 18 – 4 = 14.
- Move 4: X = 3, P2 quits as 27 > 14.
- Move 5: X = 4, P1 quits as 16 > 14.
- Game Over! Both players have quit.
Input: N = 1
Output:
Number of pens remaining in the box : 0
Number of pens collected by P1 : 1
Number of pens collected by P2 : 0
Approach: The idea is to use Recursion. Follow the steps to solve the problem:
1. Define a recursive function:
Game_Move(N, P1, P2, X, Move, QuitP1, QuitP2)
where,
N : Total number of Pens
P1 : Score of P1
P2 : Score of P2
X : Initialized to zero
Move = 0 : P1’s turn
Move = 1 : P2’s turn
QuitP1 : Has P1 quit
QuitP2 : Has P2 quit
2. Finally, print the final values after the game has ended
Below is the implementation of the above-mentioned approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int & N, int & P1, int & P2, int & X, bool Move,
bool QuitP1, bool QuitP2)
{
if (N == 0 or (QuitP1 and QuitP2)) {
cout << "Number of pens remaining"
<< " in the box: " << N << endl;
cout << "Number of pens collected"
<< " by P1: " << P1 << endl;
cout << "Number of pens collected"
<< " by P2: " << P2 << endl;
return ;
}
if (Move == 0 and QuitP1 == false ) {
int req_P1 = pow (2, X);
if (req_P1 <= N) {
P1 += req_P1;
N -= req_P1;
}
else {
QuitP1 = true ;
}
}
else if (Move == 1 and QuitP2 == false ) {
int req_P2 = pow (3, X);
if (req_P2 <= N) {
P2 += req_P2;
N -= req_P2;
}
else {
QuitP2 = true ;
}
}
X++;
Move = ((Move == 1) ? 0 : 1);
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
void PenGame( int N)
{
int P1 = 0;
int P2 = 0;
int X = 0;
bool Move = 0;
bool QuitP1 = 0;
bool QuitP2 = 0;
solve(N, P1, P2, X, Move,
QuitP1, QuitP2);
}
int main()
{
int N = 22;
PenGame(N);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GFG
{
static void solve( int N, int P1, int P2, int X,
int Move, boolean QuitP1, boolean QuitP2)
{
if (N == 0 || (QuitP1 && QuitP2))
{
System.out.println( "Number of pens remaining"
+ " in the box: " + N);
System.out.println( "Number of pens collected"
+ " by P1: " + P1);
System.out.println( "Number of pens collected"
+ " by P2: " + P2);
return ;
}
if (Move == 0 && QuitP1 == false )
{
int req_P1 = ( int )(Math.pow( 2 , X));
if (req_P1 <= N)
{
P1 += req_P1;
N -= req_P1;
}
else
{
QuitP1 = true ;
}
}
else if (Move == 1 && QuitP2 == false )
{
int req_P2 = ( int )(Math.pow( 3 , X));
if (req_P2 <= N)
{
P2 += req_P2;
N -= req_P2;
}
else
{
QuitP2 = true ;
}
}
X++;
Move = ((Move == 1 ) ? 0 : 1 );
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
static void PenGame( int N)
{
int P1 = 0 ;
int P2 = 0 ;
int X = 0 ;
int Move = 0 ;
boolean QuitP1 = false ;
boolean QuitP2 = false ;
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
public static void main (String[] args)
{
int N = 22 ;
PenGame(N);
}
}
|
Python3
def solve(N, P1, P2, X, Move,
QuitP1, QuitP2):
if (N = = 0 or (QuitP1 and QuitP2)):
print ( "Number of pens remaining in the box: " , N)
print ( "Number of pens collected by P1: " , P1)
print ( "Number of pens collected by P2: " , P2)
return
if (Move = = 0 and QuitP1 = = False ):
req_P1 = int ( pow ( 2 , X))
if (req_P1 < = N):
P1 + = req_P1
N - = req_P1
else :
QuitP1 = True
elif (Move = = 1 and QuitP2 = = False ):
req_P2 = int ( pow ( 3 , X))
if (req_P2 < = N):
P2 + = req_P2
N - = req_P2
else :
QuitP2 = True
X + = 1
if (Move = = 1 ):
Move = 0
else :
Move = 1
solve(N, P1, P2, X, Move, QuitP1, QuitP2)
def PenGame(N):
P1 = 0
P2 = 0
X = 0
Move = False
QuitP1 = False
QuitP2 = False
solve(N, P1, P2, X, Move,
QuitP1, QuitP2)
N = 22
PenGame(N)
|
C#
using System;
class GFG {
static void solve( int N, int P1, int P2, int X,
int Move, bool QuitP1, bool QuitP2)
{
if (N == 0 || (QuitP1 && QuitP2)) {
Console.WriteLine( "Number of pens remaining"
+ " in the box: " + N);
Console.WriteLine( "Number of pens collected"
+ " by P1: " + P1);
Console.WriteLine( "Number of pens collected"
+ " by P2: " + P2);
return ;
}
if (Move == 0 && QuitP1 == false ) {
int req_P1 = ( int )(Math.Pow(2, X));
if (req_P1 <= N) {
P1 += req_P1;
N -= req_P1;
}
else {
QuitP1 = true ;
}
}
else if (Move == 1 && QuitP2 == false )
{
int req_P2 = ( int )(Math.Pow(3, X));
if (req_P2 <= N)
{
P2 += req_P2;
N -= req_P2;
}
else
{
QuitP2 = true ;
}
}
X++;
Move = ((Move == 1) ? 0 : 1);
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
static void PenGame( int N)
{
int P1 = 0;
int P2 = 0;
int X = 0;
int Move = 0;
bool QuitP1 = false ;
bool QuitP2 = false ;
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
public static void Main()
{
int N = 22;
PenGame(N);
}
}
|
Javascript
<script>
function solve(N, P1, P2, X,
Move, QuitP1, QuitP2)
{
if (N == 0 || (QuitP1 && QuitP2))
{
document.write( "Number of pens remaining"
+ " in the box: " + N + "<br/>" );
document.write( "Number of pens collected"
+ " by P1: " + P1+ "<br/>" );
document.write( "Number of pens collected"
+ " by P2: " + P2+ "<br/>" );
return ;
}
if (Move == 0 && QuitP1 == false )
{
let req_P1 = (Math.pow(2, X));
if (req_P1 <= N)
{
P1 += req_P1;
N -= req_P1;
}
else
{
QuitP1 = true ;
}
}
else if (Move == 1 && QuitP2 == false )
{
let req_P2 = (Math.pow(3, X));
if (req_P2 <= N)
{
P2 += req_P2;
N -= req_P2;
}
else
{
QuitP2 = true ;
}
}
X++;
Move = ((Move == 1) ? 0 : 1);
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
function PenGame(N)
{
let P1 = 0;
let P2 = 0;
let X = 0;
let Move = 0;
let QuitP1 = false ;
let QuitP2 = false ;
solve(N, P1, P2, X, Move, QuitP1, QuitP2);
}
let N = 22;
PenGame(N);
</script>
|
Output:
Number of pens remaining in the box: 14
Number of pens collected by P1: 5
Number of pens collected by P2: 3
Time Complexity: O(Log(N))
Auxiliary Space: O(1)
Last Updated :
03 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...