Find winner of the Game where Array elements are reduced in each turn
Given a circular array arr[] of size N containing positive integers only, Player A and B are playing a game, turn by turn, the game will start with player A at index 0 and then player B at the next index.
- In every turn a player can deduct some value from arr[i] and decrease the value of an element arr[i] to X ( 0 to arr[i]-1 ).
- Then, other player turn will come and he/she will do the same with the next element in right circular order.
If a player cannot decrease the value of the element then he/she loses. Find if A and B play optimally, who will win?
Note: In each turn, a player moves to a new position than his/her previous position.
Examples:
Input: arr[] = {1, 2, 4, 3}
Output: B
Explanation: A have to deduct 1 from 1,
Now it’s B turn, B deduct whatever from 2,
Again, A deduct whatever from index 3,
Again, B deduct whatever from index 4,
Now, it’s A turn, and A is on index 0, and index 0th element is 0.
So, A will lose, B win.
Input: arr[] = {5}
Output: A
Explanation: A will decrease the value of arr[i]( 5 ) to 0.
So, for B the number become 0 and B will lose, A win.
Approach: To solve the problem follow the below idea:
The idea is, A only remove some amount from even indexed integers and B only remove some amount from odd indexed integers.
If N = odd, A will always win because
- A will remove all the amount from index 0 element making the number = 0,
- Now the other numbers whatever maybe and whatever amount will both of them remove from them, the thing is, B will always come to 0th indexed element i.e., 0, after one rotation, So, B will lose for sure,
But for N = Even, we check in what position the minimum element occur, even or odd, because
- The optimal strategy is that every one remove 1 unit from their parity element.
- And in whichever parity the first time minimum element will occur that parity element will become 0 for the first time and the player will lose and other parity element will win.
Follow the below steps to solve the problem:
- If N is Odd, A will win for sure, so just print A.
- Else find position of the first minimum element in the arr[].
- If the position is even A will win.
- Else B will win.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findAorB( int * arr, int N)
{
int K, Min = INT_MAX;
if (N & 1)
cout << "A" << endl;
else {
for ( int i = 0; i < N; i++) {
if (Min > arr[i]) {
Min = arr[i];
K = i + 1;
}
}
if (K & 1)
cout << "B" << endl;
else
cout << "A" << endl;
}
}
int main()
{
int arr[] = { 1, 2, 4, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
findAorB(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void findAorB( int arr[], int N)
{
int K = 0 , Min = Integer.MAX_VALUE;
if (N % 2 != 0 )
System.out.println( "A" );
else {
for ( int i = 0 ; i < N; i++) {
if (Min > arr[i]) {
Min = arr[i];
K = i + 1 ;
}
}
if (K % 2 != 0 )
System.out.println( "B" );
else
System.out.println( "A" );
}
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 4 , 3 };
int N = 4 ;
findAorB(arr, N);
}
}
|
Python3
INT_MAX = 2147483647
def findAorB(arr, N):
K = 0
Min = INT_MAX
if (N & 1 ):
print ( "A" )
else :
for i in range ( 0 , N):
if ( Min > arr[i]):
Min = arr[i]
K = i + 1
if (K & 1 ):
print ( "B" )
else :
print ( "A" )
if __name__ = = "__main__" :
arr = [ 1 , 2 , 4 , 3 ]
N = len (arr)
findAorB(arr, N)
|
C#
using System;
public class GFG
{
public static void findAorB( int []arr, int N)
{
int K = 0, Min = int .MaxValue;
if (N % 2 != 0)
Console.WriteLine( "A" );
else {
for ( int i = 0; i < N; i++) {
if (Min > arr[i]) {
Min = arr[i];
K = i + 1;
}
}
if (K % 2 != 0)
Console.WriteLine( "B" );
else
Console.WriteLine( "A" );
}
}
public static void Main( string [] args)
{
int []arr = { 1, 2, 4, 3 };
int N = 4;
findAorB(arr, N);
}
}
|
Javascript
<script>
function findAorB(arr,N)
{
let K, Min = Number.MAX_VALUE;
if (N & 1)
document.write( "A" );
else {
for (let i = 0; i < N; i++) {
if (Min > arr[i]) {
Min = arr[i];
K = i + 1;
}
}
if (K & 1)
document.write( "B" );
else
document.write( "A" );
}
}
let arr = [ 1, 2, 4, 3 ];
let N = arr.length;
findAorB(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
06 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...