Given an array arr[] of size N. Two players A and B are playing a game on an array arr[] and an empty sequence, Starting with player A, each player alternately removes an element from the left or right of the array and appends it to the end of the sequence. Player looses if he can not make the sequence strictly increasing on their turn. The task is to determine the winner of the game.
Example:
Input: arr[] = {5}
Output: AInput: arr[]= {5, 4, 5}
Output: AInput: arr[] = {5, 8, 2, 1, 10, 9}
Output: B
Approach: To solve the problem, follow the below idea
The greedy strategy involves each player choosing the smallest valid number from either end of the array in each turn. A number is considered valid if it is greater than the last number added to the sequence. By choosing the smallest valid number, the player maximizes the chances of being able to perform a valid operation in the next turn.
There are two cases to consider:
- When the first element is the same as the last element: In this case, it doesn’t matter which end the player chooses from, because both ends have the same number. The player will simply choose that number.
- When the first element is different from the last element: In this case, the player has two options. If both numbers are valid, the player will choose the smaller one. If only one number is valid, the player will choose that one. If neither number is valid, the player loses.
The game continues until there are no more numbers to choose from, and the player who cannot make a move loses the game.
Step-by-step approach:
- Initialize pointers i and j to the start and end of the array, respectively.
- Initialize last to -1 and turn to “A” (player A’s turn).
-
While i is less than or equal to j:
- If the number at index i is valid and smaller than or equal to the number at index j or the number at index j is invalid, choose the number at index i and update last.
- If the number at index j is valid and smaller than or equal to the number at index i or the number at index i is invalid, choose the number at index j and update last.
- If neither number is valid, return the opposite player (current player loses).
- Switch the turn.
- If the loop completes, return the player who can’t make a move (the one whose turn it is) loses.
Below is the implementation of the above approach:
#include<bits/stdc++.h> using namespace std;
// Function to determine the winner of the game string findWinner(vector< int >& arr) {
int n = arr.size(); // Size of the array
int i = 0, j = n - 1; // Pointers to the start and end of the array
int last = -1; // Last number added to the sequence
string turn = "A" ; // Player whose turn it is
// While there are still numbers to choose from
while (i <= j) {
// If the number at the start is valid and it's smaller than or equal to the number at the end
if (arr[i] > last && (arr[i] <= arr[j] || arr[j] < last)) {
last = arr[i++]; // Choose the number at the start
}
// If the number at the end is valid and it's smaller than or equal to the number at the start
else if (arr[j] > last && (arr[j] <= arr[i] || arr[i] < last)) {
last = arr[j--]; // Choose the number at the end
}
// If neither number is valid
else {
// The current player loses
return turn == "A" ? "B" : "A" ;
}
// Switch turn
turn = turn == "A" ? "B" : "A" ;
}
// The player who can't make a move loses
return turn == "A" ? "B" : "A" ;
} int main() {
vector< int > arr = {5, 8, 2, 1, 10, 9};
cout << findWinner(arr) << endl;
return 0;
} |
import java.util.*;
public class Main {
// Function to determine the winner of the game
static String findWinner(List<Integer> arr) {
int n = arr.size(); // Size of the array
int i = 0 , j = n - 1 ; // Pointers to the start and end of the array
int last = - 1 ; // Last number added to the sequence
String turn = "A" ; // Player whose turn it is
// While there are still numbers to choose from
while (i <= j) {
// If the number at the start is valid and it's smaller than or equal to the number at the end
if (arr.get(i) > last && (arr.get(i) <= arr.get(j) || arr.get(j) < last)) {
last = arr.get(i++); // Choose the number at the start
}
// If the number at the end is valid and it's smaller than or equal to the number at the start
else if (arr.get(j) > last && (arr.get(j) <= arr.get(i) || arr.get(i) < last)) {
last = arr.get(j--); // Choose the number at the end
}
// If neither number is valid
else {
// The current player loses
return turn.equals( "A" ) ? "B" : "A" ;
}
// Switch turn
turn = turn.equals( "A" ) ? "B" : "A" ;
}
// The player who can't make a move loses
return turn.equals( "A" ) ? "B" : "A" ;
}
public static void main(String[] args) {
List<Integer> arr = Arrays.asList( 5 , 8 , 2 , 1 , 10 , 9 );
System.out.println(findWinner(arr));
}
} |
# Function to determine the winner of the game def find_winner(arr):
n = len (arr) # Size of the array
i, j = 0 , n - 1 # Pointers to the start and end of the array
last = - 1 # Last number added to the sequence
turn = "A" # Player whose turn it is
# While there are still numbers to choose from
while i < = j:
# If the number at the start is valid and it's smaller than or equal to the number at the end
if arr[i] > last and (arr[i] < = arr[j] or arr[j] < last):
last = arr[i] # Choose the number at the start
i + = 1
# If the number at the end is valid and it's smaller than or equal to the number at the start
elif arr[j] > last and (arr[j] < = arr[i] or arr[i] < last):
last = arr[j] # Choose the number at the end
j - = 1
# If neither number is valid
else :
# The current player loses
return "B" if turn = = "A" else "A"
# Switch turn
turn = "B" if turn = = "A" else "A"
# The player who can't make a move loses
return "B" if turn = = "A" else "A"
# Main function if __name__ = = "__main__" :
arr = [ 5 , 8 , 2 , 1 , 10 , 9 ]
print (find_winner(arr))
|
using System;
using System.Collections.Generic;
class Program
{ // Function to determine the winner of the game
static string FindWinner(List< int > arr)
{
int n = arr.Count; // Size of the array
int i = 0, j = n - 1; // Pointers to the start and end of the array
int last = -1; // Last number added to the sequence
string turn = "A" ; // Player whose turn it is
// While there are still numbers to choose from
while (i <= j)
{
// If the number at the start is valid and it's smaller than or equal to the number at the end
if (arr[i] > last && (arr[i] <= arr[j] || arr[j] < last))
{
last = arr[i++]; // Choose the number at the start
}
// If the number at the end is valid and it's smaller than or equal to the number at the start
else if (arr[j] > last && (arr[j] <= arr[i] || arr[i] < last))
{
last = arr[j--]; // Choose the number at the end
}
// If neither number is valid
else
{
// The current player loses
return turn == "A" ? "B" : "A" ;
}
// Switch turn
turn = turn == "A" ? "B" : "A" ;
}
// The player who can't make a move loses
return turn == "A" ? "B" : "A" ;
}
static void Main()
{
List< int > arr = new List< int > { 5, 8, 2, 1, 10, 9 };
Console.WriteLine(FindWinner(arr));
}
} |
// Function to determine the winner of the game function findWinner(arr) {
let n = arr.length; // Size of the array
let i = 0, j = n - 1; // Pointers to the start and end of the array
let last = -1; // Last number added to the sequence
let turn = "A" ; // Player whose turn it is
// While there are still numbers to choose from
while (i <= j) {
// If the number at the start is valid and it's smaller than or equal to the number at the end
if (arr[i] > last && (arr[i] <= arr[j] || arr[j] < last)) {
last = arr[i++]; // Choose the number at the start
}
// If the number at the end is valid and it's smaller than or equal to the number at the start
else if (arr[j] > last && (arr[j] <= arr[i] || arr[i] < last)) {
last = arr[j--]; // Choose the number at the end
}
// If neither number is valid
else {
// The current player loses
return turn === "A" ? "B" : "A" ;
}
// Switch turn
turn = turn === "A" ? "B" : "A" ;
}
// The player who can't make a move loses
return turn === "A" ? "B" : "A" ;
} // Main function function main() {
let arr = [5, 8, 2, 1, 10, 9];
console.log(findWinner(arr));
} // Calling the main function to test the implementation main(); //This code is contriuted by Utkarsh |
B
Time Complexity: O(n)
Auxiliary Space: O(1)