
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find winner of array removal game in Python
Suppose Amal and Bimal are playing a game where they have one array A with some numbers.The game rules are as follows
- Bimal will start always
- In each turn one player deletes the maximum element from the array and all other elements present at right of the deleted element will also be deleted.
- They play alternatively
- The player who removes all remaining elements, he will win the game.
So, if the input is like nums = [5,2,6,3,4], then the output will be Amal because at first Bimal will remove [6,3,4] so array will be [5,2], then Amal will remove all, so he will be the winner.
To solve this, we will follow these steps −
- maximum := -1
- count := 0
- for each a in nums, do
- if a > maximum is non-zero, then
- count := count + 1
- maximum := a
- if a > maximum is non-zero, then
- if count mod 2 is same as 0, then
- return "Amal"
- return "Bimal"
Example
Let us see the following implementation to get better understanding −
def solve(nums): maximum = -1 count = 0 for a in nums: if a > maximum: count += 1 maximum = a if count % 2 == 0: return "Amal" return "Bimal" nums = [5,2,6,3,4] print(solve(nums))
Input
[5,2,6,3,4]
Output
Amal
- Related Articles
- C++ program to find winner of ball removal game
- Program to find winner of a set element removal game in Python
- Program to find the winner of an array game using Python
- Program to find winner of stone game in Python
- Program to find winner of number reducing game in Python
- Program to find winner of a rower reducing game in Python
- Program to find winner of a rower breaking game in Python
- Program to find maximum score of brick removal game in Python
- C++ program to find winner of card game
- Python program to find score and name of winner of minion game
- C++ program to find winner of cell coloring game
- C++ Program to find winner of unique bidding game
- C++ Program to find winner name of stick crossing game
- C++ Program to find maximum score of bit removal game
- C++ program to find winner of typing game after delay timing

Advertisements