Program to find number of moves to win deleting repeated integer game in Python

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 Amal 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. If Amal selects [1, 1, 3], then Bimal removes 3 to maximize turns, the array becomes [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] with no duplicate elements.

Algorithm

To solve this problem, we follow these steps:

  • Count the number of repeated elements by comparing adjacent elements
  • Return (repeats + 1) // 2 as the minimum number of turns

The key insight is that in each turn, Amal can eliminate at most 2 duplicate elements (one directly, and force Bimal to help eliminate another). Since Bimal acts to maximize turns, the optimal strategy results in (repeats + 1) // 2 turns.

Example

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

# Test the solution
ob = Solution()
nums = [1, 1, 2, 3, 3, 3, 4]
result = ob.solve(nums)
print(f"Input: {nums}")
print(f"Number of turns needed: {result}")
Input: [1, 1, 2, 3, 3, 3, 4]
Number of turns needed: 2

How It Works

In the example [1, 1, 2, 3, 3, 3, 4]:

  • Comparing adjacent elements: nums[1] == nums[0] (1 == 1), nums[4] == nums[3] (3 == 3), nums[5] == nums[4] (3 == 3)
  • Total repeats = 3
  • Minimum turns = (3 + 1) // 2 = 2

Alternative Implementation

Here's a more functional approach using list comprehension:

def min_turns_to_remove_duplicates(nums):
    """Calculate minimum turns to remove all duplicate elements."""
    repeats = sum(1 for i in range(1, len(nums)) if nums[i] == nums[i-1])
    return (repeats + 1) // 2

# Test with multiple examples
test_cases = [
    [1, 1, 2, 3, 3, 3, 4],
    [1, 2, 2, 2, 3],
    [1, 1, 1, 1, 1]
]

for nums in test_cases:
    turns = min_turns_to_remove_duplicates(nums)
    print(f"Array: {nums} ? Turns needed: {turns}")
Array: [1, 1, 2, 3, 3, 3, 4] ? Turns needed: 2
Array: [1, 2, 2, 2, 3] ? Turns needed: 2
Array: [1, 1, 1, 1, 1] ? Turns needed: 2

Conclusion

The solution counts adjacent duplicate pairs and applies the formula (repeats + 1) // 2 to find the minimum turns needed. This accounts for optimal play where Amal minimizes turns while Bimal tries to maximize them.

Updated on: 2026-03-25T11:00:54+05:30

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements