- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose two friends Amal and Bimal are playing a game with a sorted list of numbers called nums. In this game in a single turn, Amal chooses any three numbers. Bimal removes one of them, and then Amal removes one of them. The list starts out with an odd number of elements. Here Amla wishes to minimize the number of turns required to make the list contain no repeated elements, Bimal wishes to maximize the number of turns. If Amal and Bimal act optimally, we have to find how many turns are needed for this game.

So, if the input is like nums = [1, 1, 2, 3, 3, 3, 4], then the output will be 2, as If Amal selects [1, 1, 3], then Bimal removes 3 to maximize turns, the array is [1, 1, 2, 3, 3, 4], Amal removes 1, so array is [1,2,3,3,4], then in the next turn Amal selects [3,3,4], then Bimal will remove 4 to maximize turns. So then Amal can remove 3 and array will be [1,2,3], there is no duplicate elements.

To solve this, we will follow these steps −

repeats := 0

for i in range 1 to size of nums, do

if nums[i] is same as nums[i-1], then

repeats := repeats + 1

return quotient of (repeats + 1) / 2

Let us see the following implementation to get better understanding −

class Solution: def solve(self, nums): repeats = 0 for i in range(1, len(nums)): if nums[i] == nums[i-1]: repeats += 1 return (repeats + 1) // 2 ob = Solution() nums = [1, 1, 2, 3, 3, 3, 4] print(ob.solve(nums))

[1, 1, 2, 3, 3, 3, 4]

2

- Related Questions & Answers
- Program to find number of expected moves required to win Lotus and Caterpillar game in Python
- Program to find number of possible moves to start the game to win by the starter in Python
- Program to find out if we win in a game in Python
- Program to find winner of number reducing game in Python
- Program to find out the minimum moves in a snakes and ladders game in Python
- Program to check whether Amal can win stone game or not in Python
- Program to find winner of stone game in Python
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- Program to check whether first player win in candy remove game or not in Python?
- Minimum Players required to win the game in C++
- Program to find winner of array removal game in Python
- Program to find maximum score of brick removal game in Python
- Program to find winner of a rower breaking game in Python
- Program to find winner of a rower reducing game in Python
- Program to find minimum moves to make array complementary in Python

Advertisements