<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>

# class Solution:
#     def minimumOperations(self, nums: List[int]) -> int:
#         if len(nums) == 1:
#             return 0
#         odd_dict = {}
#         even_dict = {}
#         for i in range(0, len(nums)):
#             if i % 2 == 0:
#                 if nums[i] not in even_dict:
#                     even_dict[nums[i]] = 1
#                 else:
#                     even_dict[nums[i]] += 1
#             elif i % 2 == 1:
#                 if nums[i] not in odd_dict:
#                     odd_dict[nums[i]] = 1
#                 else:
#                     odd_dict[nums[i]] += 1
#         most_frequent_even_number = max(even_dict, key=even_dict.get)
#         print(even_dict, odd_dict, sorted(odd_dict, key=odd_dict.get))
#         second_most_frequent_odd_number = sorted(odd_dict, key=odd_dict.get)[1] 

#         # even_dict_sorted = sorted(even_dict, key=even_dict.get)
#         # even_dict_sorted[]   
#         most_frequent_odd_number = max(odd_dict, key=odd_dict.get)
#         second_most_frequent_odd_number = sorted(odd_dict, key=odd_dict.get)[1] 
#         print(most_frequent_even_number, most_frequent_odd_number)

#         # count = 0
#         # for i in range(0, len(nums)):
#         #     if i % 2 == 0:
#         #         if nums[i] != most_frequent_even_number:
#         #             nums[i] = most_frequent_even_number
#         #             count += 1
#         #     if i % 2 == 1:
#         #         if nums[i] != most_frequent_odd_number:
#         #             nums[i] = most_frequent_odd_number
#         #             count += 1
        
#         if most_frequent_even_number != most_frequent_odd_number:
#             return len(nums) - max(most_frequent_odd_number, most_frequent_even_number)
#         else:
#             return len(nums) - max((most_frequent_even_number + second_most_frequent_odd_number), 
#                 (most_frequent_odd_number + second_most_frequent_even_number))



from collections import Counter

class Solution:
    def minimumOperations(self, nums):
        # Split the array into two lists: even-indexed and odd-indexed elements
        odd = [nums[i] for i in range(len(nums)) if i % 2 != 0]
        even = [nums[i] for i in range(len(nums)) if i % 2 == 0]

        # Get the most frequent and second most frequent elements and their counts
        frequency_even = self.get_highest_and_second_highest_pair(even)
        frequency_odd = self.get_highest_and_second_highest_pair(odd)

        # Case 1: If the most frequent element in even and odd arrays are different
        if frequency_even[0] != frequency_odd[0]:
            return len(nums) - (frequency_even[1] + frequency_odd[1])
        else:
            # Case 2: When the most frequent element is the same in both arrays
            X1 = frequency_even[1] + frequency_odd[2]  # Convert even to max, odd to second max
            X2 = frequency_even[2] + frequency_odd[1]  # Convert odd to max, even to second max
            return len(nums) - max(X1, X2)

    def get_highest_and_second_highest_pair(self, arr):
        # Use a Counter to count frequencies of elements
        frequency = Counter(arr)
        # Find the most common element and its frequency
        most_common = frequency.most_common(2)  # Get the top two most common elements
        
        first = most_common[0][0] if most_common else 0
        first_count = most_common[0][1] if most_common else 0
        second_count = most_common[1][1] if len(most_common) > 1 else 0
        
        return [first, first_count, second_count]

865