DEV Community

Cover image for Yet Another Number Game
Gourav Kadu
Gourav Kadu

Posted on

Yet Another Number Game

Statment

Alice and Bob play the following game. They choose a number N to play with. The rules are as follows :
1) Alice plays first, and the two players alternate.
2) In his/her turn, a player can subtract from N any proper divisor (not equal to N) of N. The number thus obtained is the new N.
3) The person who cannot make a move in his/her turn loses the game.
Assuming both play optimally, who wins the game ?

Input :

The first line contains the number of test cases T. Each of the next T lines contains an integer N.

Output :

Output T lines, one for each test case, containing "ALICE" if Alice wins the game, or "BOB" otherwise.

Sample Input :

2
1
2
Enter fullscreen mode Exit fullscreen mode

Sample Output :

BOB
ALICE
Enter fullscreen mode Exit fullscreen mode

Constraints :
1 <= T <= 10000
1 <= N <= 1000000000

Note : For the first test case, Alice cannot make any move and hence Bob wins the game. For the second test case, Alice subtracts 1 from N. Now, Bob cannot make a move and loses the game.

Solution:-

To run the code click here.

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
     Scanner sc = new Scanner(System.in);  

        int T = sc.nextInt();  
        int arr[] = new int[T];
        int count=0;
        for(int i = 0; i <T; i++) {
            arr[i]= sc.nextInt();
        }
        for(int i = 0; i <T; i++) {
            if(arr[i]==1) {
                System.out.println("BOB");
            }else if(arr[i]%2==1) {
                System.out.println("BOB");
            }else {
                System.out.println("ALICE");
            }
        }
        sc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)