Skip to content
Related Articles
Open in App
Not now

Related Articles

Find the winner of game where X picks 1, then Y picks 2, then X picks 3 and so on

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 28 Feb, 2023
Improve Article
Save Article
Like Article

Two players X and Y are picking up numbers alternatively with X picking first. In the first turn X picks 1, then Y picks 2, then X picks 3 and the game goes on like this. When a player cannot pick a number he loses the game. Given 2 integers A and B denoting the maximum sum of the numbers X and Y can pick respectively. Find the winner of the game. 

Examples:

Input: A = 3, B = 2
Output: Y
Explanation: Initially, X picks 1, Y picks 2. 
Now in third move X can only pick 3, thereby making the sum (1+3) = 4.
4 exceed total permissible sum for X i.e. 3. So the winner is Y 

Input: A = 4, B = 2
Output: X 

 

Approach: The task can be solved by maintaining 2 sums, one for X and one for Y. Follow the below steps to solve the problem:

  • The maximum limit of X and Y is A and B respectively.
  • Initialize the total sum for X and Y with 0.
  • Initialize counter with 0.
  • Run while loop until (total_x ≤ A and total_y ≤ B)
  • Increment counter
  • After loop break, check who crossed the limit first

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the winner
void winner(int A, int B)
{
    // Initialize with zero
    int total_x, total_y, counter = 0;
 
    // Fixed limit
    while (total_x <= A && total_y <= B) {
        // Increment counter
        counter = counter + 1;
 
        // X's total
        total_x = total_x + counter;
 
        // Increment counter
        counter = counter + 1;
 
        // Y's total
        total_y = total_y + counter;
    }
    if (total_x > A && total_y > B)
        cout << "Y";
    else if (total_x > A)
        cout << "Y";
    else
        cout << "X";
}
 
// Driver Code
int main()
{
    int A = 3, B = 2;
    winner(A, B);
    return 0;
}

C




// C program for the above approach
#include <stdio.h>
 
// Function to find the winner
void winner(int A, int B)
{
    // Initialize with zero
    int total_x, total_y, counter = 0;
 
    // Fixed limit
    while (total_x <= A && total_y <= B) {
        // Increment counter
        counter = counter + 1;
 
        // X's total
        total_x = total_x + counter;
 
        // Increment counter
        counter = counter + 1;
 
        // Y's total
        total_y = total_y + counter;
    }
    if (total_x > A && total_y > B)
        printf("Y");
    else if (total_x > A)
        printf("Y");
    else
        printf("X");
}
 
// Driver Code
void main()
{
    int A = 3, B = 2;
    winner(A, B);
}

Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the winner
  static void winner(int A, int B)
  {
    // Initialize with zero
    int total_x = 0, total_y = 0, counter = 0;
 
    // Fixed limit
    while (total_x <= A && total_y <= B) {
      // Increment counter
      counter = counter + 1;
 
      // X's total
      total_x = total_x + counter;
 
      // Increment counter
      counter = counter + 1;
 
      // Y's total
      total_y = total_y + counter;
    }
    if (total_x > A && total_y > B)
      System.out.println("Y");
    else if (total_x > A)
      System.out.println("Y");
    else
      System.out.println("X");
  }
 
  // Driver Code
  public static void main (String[] args) {
    int A = 3, B = 2;
    winner(A, B);
  }
}
 
// This code is contributed by hrithikgarg03188.

Python




# Python program for the above approach
 
# Function to find the winner
def winner(A, B):
     
    # Initialize with zero
    total_x = 0
    total_y = 0
    counter = 0
 
    # Fixed limit
    while (total_x <= A and total_y <= B):
         
        # Increment counter
        counter = counter + 1
 
        # X's total
        total_x = total_x + counter
 
        # Increment counter
        counter = counter + 1
 
        # Y's total
        total_y = total_y + counter
     
    if (total_x > A and total_y > B):
        print("Y")
    elif (total_x > A):
        print("Y")
    else:
        print("X")
 
# Driver Code
 
A = 3
B = 2
winner(A, B)
 
# This code is contributed by Samim Hossain Mondal.

C#




// C# program for the above approach
using System;
class GFG {
 
  // Function to find the winner
  static void winner(int A, int B)
  {
    // Initialize with zero
    int total_x = 0, total_y = 0, counter = 0;
 
    // Fixed limit
    while (total_x <= A && total_y <= B) {
      // Increment counter
      counter = counter + 1;
 
      // X's total
      total_x = total_x + counter;
 
      // Increment counter
      counter = counter + 1;
 
      // Y's total
      total_y = total_y + counter;
    }
    if (total_x > A && total_y > B)
      Console.WriteLine("Y");
    else if (total_x > A)
      Console.WriteLine("Y");
    else
      Console.WriteLine("X");
  }
 
  // Driver Code
  public static void Main () {
    int A = 3, B = 2;
    winner(A, B);
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript




<script>
    // JavaScript code for the above approach
 
    // Function to find the winner
    function winner(A, B)
    {
     
        // Initialize with zero
        let total_x, total_y, counter = 0;
 
        // Fixed limit
        while (total_x <= A && total_y <= B)
        {
         
            // Increment counter
            counter = counter + 1;
 
            // X's total
            total_x = total_x + counter;
 
            // Increment counter
            counter = counter + 1;
 
            // Y's total
            total_y = total_y + counter;
        }
        if (total_x > A && total_y > B)
            document.write("Y")
        else if (total_x > A)
            document.write("Y")
        else
            document.write("X")
    }
 
    // Driver Code
    let A = 3, B = 2;
    winner(A, B);
 
   // This code is contributed by Potta Lokesh
</script>

 
 

Output

Y

Time Complexity: O(A+B)
Auxiliary Space: O(1)

Another Approach: 

  • Start by initializing the maximum limit for X and Y as A and B respectively.
  • Initialize the total sum for X and Y as 0.
  • Initialize a boolean flag x_wins as true.
  • Use a for loop to iterate over the numbers X and Y pick.
  • In each iteration, add the current number to the total sum for X and Y.
  • If the total sum for X exceeds A, set x_wins to false and break the loop.
  • If the total sum for Y exceeds B, set x_wins to true and break the loop.
  • After the loop, check the value of x_wins. If it is true, X wins, else Y wins.
  • Print the winner of the game.

Below is the implementation of above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the winner
void winner(int A, int B)
{
// Initialize with zero
int total_x = 0, total_y = 0;
bool x_wins = true;
// Iterate over the numbers X and Y pick
for(int i = 1; total_x <= A && total_y <= B; i += 2) {
    total_x += i;
    if(total_x > A) {
        x_wins = false;
        break;
    }
    total_y += i+1;
    if(total_y > B) {
        x_wins = true;
        break;
    }
}
if(x_wins)
    cout << "X";
else
    cout << "Y";
}
 
// Driver Code
int main()
{
int A = 3, B = 2;
winner(A, B);
return 0;
}

Java




public class GameWinner {
 
    public static void winner(int A, int B) {
        int total_x = 0, total_y = 0;
        boolean x_wins = true;
 
        for(int i = 1; total_x <= A && total_y <= B; i += 2) {
            total_x += i;
            if(total_x > A) {
                x_wins = false;
                break;
            }
            total_y += i+1;
            if(total_y > B) {
                x_wins = true;
                break;
            }
        }
 
        if(x_wins)
            System.out.println("X");
        else
            System.out.println("Y");
    }
 
    public static void main(String[] args) {
        int A = 3, B = 2;
        winner(A, B);
    }
}

C#




using System;
 
public class GameWinner
{
    public static void winner(int A, int B)
    {
        int total_x = 0, total_y = 0;
        bool x_wins = true;
 
        for(int i = 1; total_x <= A && total_y <= B; i += 2) {
            total_x += i;
            if(total_x > A) {
                x_wins = false;
                break;
            }
            total_y += i+1;
            if(total_y > B) {
                x_wins = true;
                break;
            }
        }
 
        if(x_wins)
            Console.WriteLine("X");
        else
            Console.WriteLine("Y");
    }
 
    public static void Main()
    {
        int A = 3, B = 2;
        winner(A, B);
    }
}

Python3




def winner(A, B):
    total_x = 0
    total_y = 0
    x_wins = True
 
    for i in range(1, A+B+1, 2):
        total_x += i
        if total_x > A:
            x_wins = False
            break
        total_y += i+1
        if total_y > B:
            x_wins = True
            break
 
    if x_wins:
        print("X")
    else:
        print("Y")
 
A = 3
B = 2
winner(A, B)

Javascript




function winner(A, B) {
    let total_x = 0;
    let total_y = 0;
    let x_wins = true;
 
    for(let i = 1; total_x <= A && total_y <= B; i += 2) {
        total_x += i;
        if(total_x > A) {
            x_wins = false;
            break;
        }
        total_y += i+1;
        if(total_y > B) {
            x_wins = true;
            break;
        }
    }
 
    if(x_wins)
        console.log("X");
    else
        console.log("Y");
}
 
let A = 3;
let B = 2;
winner(A, B);

Output

Y

Time Complexity: O(A+B)

Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!