Thursday, May 19, 2022

Easy_Question23 : Divisor game

Given an integer N and two players, A and B are playing a game. On each player’s turn, that player makes a move by subtracting a divisor of current N (which is less than N) from current N, thus forming a new N for the next turn.

The player who does not have any divisor left to subtract loses the game. The task is to tell which player wins the game if player A takes the first turn, assuming both players play optimally.


Input : N = 2 
Output : Player A wins 
Explanation : 
Player A chooses 1, and B has no more moves.
Input : N = 3 
Output : Player B wins 
Explanation : 
Player A chooses 1, player B chooses 1, and A has no more moves. 

public class TechTwitter{ public static void main(String[] args) { System.out.print("Player A wins " + recursive_check(15)); } static boolean recursive_check(int n) { if(n == 1) return false; if(n == 2) return true; for(int i=1; i<=n/2; i++) { if(n%i == 0 && !recursive_check(n - i)) { return true; } } return false; } }


Follow on LinkedIn

You may also like

Kubernetes AWS Java Coding Question
Microservices Core Java Spring Boot
Spring Framework Kafka Miscellaneous