<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <code>n</code> positive integers.</p>

<p>The array <code>nums</code> is called <strong>alternating</strong> if:</p>

<ul>
	<li><code>nums[i - 2] == nums[i]</code>, where <code>2 &lt;= i &lt;= n - 1</code>.</li>
	<li><code>nums[i - 1] != nums[i]</code>, where <code>1 &lt;= i &lt;= n - 1</code>.</li>
</ul>

<p>In one <strong>operation</strong>, you can choose an index <code>i</code> and <strong>change</strong> <code>nums[i]</code> into <strong>any</strong> positive integer.</p>

<p>Return <em>the <strong>minimum number of operations</strong> required to make the array alternating</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,1,3,2,4,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
One way to make the array alternating is by converting it to [3,1,3,<u><strong>1</strong></u>,<u><strong>3</strong></u>,<u><strong>1</strong></u>].
The number of operations required in this case is 3.
It can be proven that it is not possible to make the array alternating in less than 3 operations. 
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2,2,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
One way to make the array alternating is by converting it to [1,2,<u><strong>1</strong></u>,2,<u><strong>1</strong></u>].
The number of operations required in this case is 2.
Note that the array cannot be converted to [<u><strong>2</strong></u>,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

from collections import Counter
from typing import List

class Solution:
    def minimumOperations(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return 0
        
        # Separate nums into even and odd indexed elements
        even_elements = nums[0::2]
        odd_elements = nums[1::2]
        
        # Count frequencies of elements in even and odd indexed positions
        even_count = Counter(even_elements)
        odd_count = Counter(odd_elements)
        
        # Get the most common elements and their counts
        even_most_common = even_count.most_common(2)
        odd_most_common = odd_count.most_common(2)
        
        # Fill in missing second most common if necessary
        if len(even_most_common) < 2:
            even_most_common.append((None, 0))
        if len(odd_most_common) < 2:
            odd_most_common.append((None, 0))
        
        # Extract the most common and second most common elements and their counts
        even1, even1_count = even_most_common[0]
        even2, even2_count = even_most_common[1]
        odd1, odd1_count = odd_most_common[0]
        odd2, odd2_count = odd_most_common[1]
        
        # Calculate the minimum operations needed
        n = len(nums)
        if even1 != odd1:
            # If the most common elements are different, use them
            min_operations = (n // 2 - even1_count) + ((n + 1) // 2 - odd1_count)
        else:
            # If they are the same, choose the best alternative
            min_operations = min(
                (n // 2 - even1_count) + ((n + 1) // 2 - odd2_count),
                (n // 2 - even2_count) + ((n + 1) // 2 - odd1_count)
            )
        
        return min_operations
33400