Given numeric string str, the task is to determine the winner of the game when two players play a game optimally with the string as per the given conditions:
- Player 1 always starts first.
- In one turn one player can remove the contiguous elements from the string and the number of elements removed will add up to his score. Player 1 will remove odd contiguous elements and Player 1 will remove even contiguous elements.
- Any player who is not able to make a move loses the game.
- After all the strings are removed, the player with maximum scores wins the game and if scores are equal then print “-1”.
Input: str = “7788”
First move for player 1 is to remove 77 and for player 2 is 88. Score for both is 2. Hence -1.
Input: str = “8822”
Output: Player 2
There are no odd elements so player 1 cant make amove so player 2 wins.
Approach: Follow the steps below in order to solve the problem:
- Create an array A of size 10 to store the frequency of all the digits in the given string.
- Iterate the given string and update the frequency of each digit in the above array.
- Traverse the array A and take two variables as sum1 = 0 and sum2 = 0 to store the sum of scores.
- Increment sum1 if the index is odd i.e., Player 1 turn else increment sum2 if the index is even i.e., Player 2 turn.
- After the above steps check if sum1 is equal to sum2 then print -1 otherwise print the player number who wins the game on the basis of the score.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Determine winner of the Game by arranging balls in a row
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Find the winner of a game where scores are given as a binary string
- Find the winner in nim-game
- Find the winner of the Game to Win by erasing any two consecutive similar alphabets
- Find the winner of the game with N piles of boxes
- Predict the winner of the game | Sprague-Grundy
- Predict the winner in Coin Game
- Predict the winner of the game on the basis of absolute difference of sum by selecting numbers
- Find the winner of the game
- Find the winner of the Game
- Winner in the Rock-Paper-Scissor game using Bit manipulation
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Find the winner of the game based on greater number of divisors
- Find the winner of the Game of removing odd or replacing even array elements
- Predict the winner of a card game of removing K cards in each turn such that Bitwise AND of K and size of pile is 0
- Minimum steps to delete a string by deleting substring comprising of same characters
- Largest string obtained in Dictionary order after deleting K characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.