Find Winner of Game by Making the Sequence Strictly Increasing
Last Updated :
05 Mar, 2024
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: A
Input: arr[]= {5, 4, 5}
Output: A
Input: 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:
C++
#include<bits/stdc++.h>
using namespace std;
string findWinner(vector< int >& arr) {
int n = arr.size();
int i = 0, j = n - 1;
int last = -1;
string turn = "A" ;
while (i <= j) {
if (arr[i] > last && (arr[i] <= arr[j] || arr[j] < last)) {
last = arr[i++];
}
else if (arr[j] > last && (arr[j] <= arr[i] || arr[i] < last)) {
last = arr[j--];
}
else {
return turn == "A" ? "B" : "A" ;
}
turn = turn == "A" ? "B" : "A" ;
}
return turn == "A" ? "B" : "A" ;
}
int main() {
vector< int > arr = {5, 8, 2, 1, 10, 9};
cout << findWinner(arr) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static String findWinner(List<Integer> arr) {
int n = arr.size();
int i = 0 , j = n - 1 ;
int last = - 1 ;
String turn = "A" ;
while (i <= j) {
if (arr.get(i) > last && (arr.get(i) <= arr.get(j) || arr.get(j) < last)) {
last = arr.get(i++);
}
else if (arr.get(j) > last && (arr.get(j) <= arr.get(i) || arr.get(i) < last)) {
last = arr.get(j--);
}
else {
return turn.equals( "A" ) ? "B" : "A" ;
}
turn = turn.equals( "A" ) ? "B" : "A" ;
}
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));
}
}
|
Python
def find_winner(arr):
n = len (arr)
i, j = 0 , n - 1
last = - 1
turn = "A"
while i < = j:
if arr[i] > last and (arr[i] < = arr[j] or arr[j] < last):
last = arr[i]
i + = 1
elif arr[j] > last and (arr[j] < = arr[i] or arr[i] < last):
last = arr[j]
j - = 1
else :
return "B" if turn = = "A" else "A"
turn = "B" if turn = = "A" else "A"
return "B" if turn = = "A" else "A"
if __name__ = = "__main__" :
arr = [ 5 , 8 , 2 , 1 , 10 , 9 ]
print (find_winner(arr))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static string FindWinner(List< int > arr)
{
int n = arr.Count;
int i = 0, j = n - 1;
int last = -1;
string turn = "A" ;
while (i <= j)
{
if (arr[i] > last && (arr[i] <= arr[j] || arr[j] < last))
{
last = arr[i++];
}
else if (arr[j] > last && (arr[j] <= arr[i] || arr[i] < last))
{
last = arr[j--];
}
else
{
return turn == "A" ? "B" : "A" ;
}
turn = turn == "A" ? "B" : "A" ;
}
return turn == "A" ? "B" : "A" ;
}
static void Main()
{
List< int > arr = new List< int > { 5, 8, 2, 1, 10, 9 };
Console.WriteLine(FindWinner(arr));
}
}
|
Javascript
function findWinner(arr) {
let n = arr.length;
let i = 0, j = n - 1;
let last = -1;
let turn = "A" ;
while (i <= j) {
if (arr[i] > last && (arr[i] <= arr[j] || arr[j] < last)) {
last = arr[i++];
}
else if (arr[j] > last && (arr[j] <= arr[i] || arr[i] < last)) {
last = arr[j--];
}
else {
return turn === "A" ? "B" : "A" ;
}
turn = turn === "A" ? "B" : "A" ;
}
return turn === "A" ? "B" : "A" ;
}
function main() {
let arr = [5, 8, 2, 1, 10, 9];
console.log(findWinner(arr));
}
main();
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...