Skip to content
Related Articles

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
  • Difficulty Level : Medium
  • Last Updated : 08 Feb, 2022
Improve Article
Save 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(log N)
Auxiliary Space: O(1)

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!