Last Updated on June 17, 2022 by Ria Pathak
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 <stdio.h> void solve() { long long int n; long long int x; scanf("%lld",&n); scanf("%lld",&x); long long int result=x; for(long long int j=1;j<n;j++) { scanf("%lld",&x); result=result^x; } if(result==0) printf("Friend\n"); else printf("Arnab\n"); } int main() { int t; scanf("%d",&t); while(t--) { solve(); } return 0; }
#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; }
import java.util.*; class PlayGames3 { static Scanner sc=new Scanner(System.in); static void solve() { long n=sc.nextLong(); long x=sc.nextLong(); long result=x; for(int j=1;j<n;j++) { x=sc.nextLong(); result=result^x; } if(result==0) { System.out.println("Friend"); } else { System.out.println("Arnab"); } } public static void main(String[] args) { int t=sc.nextInt(); while(t!=0) { solve(); t--; } } }
def solve(): n = int(input()) a = list(map(int,input().split())) result = a[0] for j in range(1, n): result = result ^ a[j] if(result == 0): print("Friend") else: print("Arnab") for _ in range(int(input())): solve()
[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.