Play Games 4

CONCEPTS USED:

Game theory

DIFFICULTY LEVEL:

Medium.

PROBLEM STATEMENT(SIMPLIFIED):

Arnab wants to play games with his friend. Now this time Arnab made an array of integers that represent the piles of chocolates. So in each turn, a player can choose only one pile and remove any number of chocolates (greater than or equal to one) from that pile. The player who cannot make a move loses the game. Arnab takes the first move. Both players play optimally.

See original problem statement here

For Example :

Input
1
5 
1 2 3 4 5 

Output
Arnab

OBSERVATION:

Take the xor of all the numbers given.

If the xor comes out to be 0,then the friend wins.

If it is not zero,then Arnab wins.

SOLUTION:

#include <bits/stdc++.h>
using namespace std;
void solve()
{
  long long int n;
        long long int x;
        cin>>n;
        cin>>x;
        long long int result=x;
        for(long long int j=1;j<n;j++)
        {
            cin>>x;
            result=result^x;
        }
        if(result==0)
          cout<<"Friend"<<endl;
        else
          cout<<"Arnab"<<endl;
}
int main()
{  
  int t;
  cin>>t;
  while(t--)
  {
    solve();
  }

return 0;
}

[forminator_quiz id="2347"]

So, in this blog, we have tried to explain the concept of Game Theory. If you want to solve more questions on Game Theory, which are curated by our expert mentors at PrepBytes, you can follow this link Game Theory.

Leave a Reply

Your email address will not be published. Required fields are marked *