Here is another question which is asked by many people, this is a question asked in codevita. This codevita answer is provided in C++ and C programming language. You can also convert it to java and other programming languages. Lets begin with the coding.
Codevita Question
Aman and Jasbir are very intelligent guys of their batch. Today they are playing a game “Game of Numbers“.
Game Description:
There are N numbers placed on a table. Since two players are playing the game, they will make their moves alternatively. In one move a player can perform the following operation.
- A player will choose a number from the table and will replace that number with one of its divisor. For example, 6 can be replaced with 1, 2, or 3 (since these are the divisors of 6). Similarly, 12 can be replaced with 1, 2, 3, 4 or 6.
- It is mandatory that the player has to replace the number.
- A player cannot put back the same number on table.
- As 1 does not have any divisor other than itself, a player cannot replace 1 with any other number. So soon a situation will arise when there will be only all 1s on the table. In that situation the player will not be able to make any move. The player who will not be able to make the move, loses.
- Both the players are masters of this game. So they will play the game optimally.
- Aman will make the first move of the game.
As you will be given N integers that are on the table. You have to predict, who will win the game – Aman or Jasbir.
Input Format:
- First line contains information about the numbers present of the table, denoted by N
- Second line contains N integers delimited by space – A1 A2 A3 A4 ——– AN
Output Format:
- Print the name of the player who will win the game in upper case i.e. AMAN or JASBIR.
Constraints:
- 1 <= N <= 100000
- 0 < Ai <= 10000
- A player cannot replace more than 1 number in one move.
- Players move alternate and a player cannot pass or make no move.
- A number cannot be replaced itself, i.e. 6 can be replaced with 1, 2 or 3 only , not with 6.
- Aman always makes the first move.
Sample Input and Output for this coding problem
The explanation for sample input and output
Codevita sample #1 Explanation:
There is only one number placed on the table do AMAN will replace it with 1. Jasbir will play next. As there is only 1 left on the table. And it cannot be replaced with any other number; he is not able to make any move. So he loses the game.
Also read: TCS codevita old questions for practice
Codevita sample #2 Explanation:
Codevita answer in C++ Programming
# include<iostream>
using namespace std;
int main()
{
int N,i,sum=0; //N as an int
int arr[100000];
cin>>N; //Entering the value of N
for(i=0;i<N;i++){
cin>>arr[i];} //Entering the value in array
for(i=0;i<N;i++){
sum=sum^arr[i]; //Logical value
}
if(sum==0)
cout<<“JASBIR”; //Output the Name of winner
else
cout<<“AMAN”;
return 0;
}
Codevita answer in C programming
# include<stdio.h>
int main(void)
{
int N,i,sum=0; //N as an int
int arr[100000];
scanf(“%d”,&N); //Entering the value of N
for(i=0;i<N;i++){
scanf(“%d”,&arr[i]);} //Entering the value in array
for(i=0;i<N;i++){
sum=sum^arr[i]; //Logical value
}
if(sum==0)
printf(“JASBIR”); //Output the winner name
else
printf(“AMAN”);
return 0;
}
Hope you liked this post. Ask your query in the comment below. If you think it’s not correct or have any issues. Ask or tell it in the comment section below the post.