<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

class Solution:
    def minimumOperations(self, nums: List[int]) -> int:
        n = len(nums)
        
        # If the array has only one element, no operations are needed.
        if n == 1:
            return 0
        
        # Function to count operations needed to make the array alternating with a given starting element.
        def count_operations(start_value):
            operations = 0
            for i in range(0, n, 2):
                if nums[i] != start_value:
                    operations += 1
            return operations
        
        # Count the frequency of elements in the even and odd positions.
        even_counts = Counter(nums[0::2])
        odd_counts = Counter(nums[1::2])
        
        # Find the most common elements in even and odd positions.
        most_common_even = even_counts.most_common(2)
        most_common_odd = odd_counts.most_common(2)
        
        # If the most common elements are different, we can use them directly.
        if most_common_even[0][0] != most_common_odd[0][0]:
            return n - most_common_even[0][1] - most_common_odd[0][1]
        else:
            # If the most common elements are the same, we need to consider the second most common elements.
            if len(most_common_even) == 1 and len(most_common_odd) == 1:
                # If there is only one common element in both positions, we need to change one of them.
                return n - max(most_common_even[0][1], most_common_odd[0][1])
            elif len(most_common_even) == 1:
                # If there is only one common element in even positions, use the second most common in odd positions.
                return n - most_common_even[0][1] - most_common_odd[1][1]
            elif len(most_common_odd) == 1:
                # If there is only one common element in odd positions, use the second most common in even positions.
                return n - most_common_even[1][1] - most_common_odd[0][1]
            else:
                # If there are multiple common elements in both positions, choose the best pair.
                return n - max(most_common_even[0][1] + most_common_odd[1][1], most_common_even[1][1] + most_common_odd[0][1])

# Example usage:
solution = Solution()
nums = [3,1,3,2,4,3]
print(solution.minimumOperations(nums))  # Output: 3

nums = [1,2,2,2,2]
print(solution.minimumOperations(nums))  # Output: 2
809