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

'''

Approach:

Minimum number of operations -> least number of changes needed

Get most frequent element, second most freq elem in even positions
Get most frequent element, second most freq elem in odd positions

even - idx = [0,2,4,...]
odd  - idx = [1,3,5,...]

1st_even = a
1st_odd =  b

2nd_even = c - needs to be different
2nd_odd = d - 



if a != b:
    get_missing_count_even - number of other elems in non 1st_even slots
    get_missing_count_odd - number of other elems in non 1st_odd slots
    return get_missing_count_even + get_missing_count_even

else: # 1st_even == 1st_odd
    if 1st_even_cnt > 1st_odd_cnt:
        Use 1st_even_cnt and 2nd_odd_cnt as above
    else:
        Use 2nd_even_cnt and 1st_odd_cnt as above


Algo:

even_elem_dict = collections.defaultdict(int)
odd_elem_dict = collections.defaultdict(int)

total_even = 0
total_odd = 0

for i in range(len(nums)):
    if i % 2 == 0: # if even
        total_even += 1
        even_elem_dict[nums[i]] += 1
    else:
        total_odd += 1
        odd_elem_dict[nums[i]] += 1








'''

import heapq
import collections


class Solution:
    def minimumOperations(self, nums: List[int]) -> int:

        if len(nums) == 1:
            return 0


        even_elem_dict = collections.defaultdict(int)
        odd_elem_dict = collections.defaultdict(int)

        total_even = 0
        total_odd = 0

        for i in range(len(nums)):
            if i % 2 == 0: # if even
                total_even += 1
                even_elem_dict[nums[i]] += 1
            else:
                total_odd += 1
                odd_elem_dict[nums[i]] += 1
        #i=0 1 2 3 4 5 6 7
        # [2,1,3,1,1,3,1,3]
        # even_elem_dict={2:1, 3:1, 1:2  6:1}
        # odd_elem_dict={1:2, }

        odd = sorted(((v, k) for k, v in odd_elem_dict.items()))
        even = sorted(((v, k) for k, v in even_elem_dict.items()))

        
     #   if no repeat, then just choose the high freq
        if odd[-1][1] != even[-1][1]: return len(nums) - odd[-1][0] - even[-1][0]
        
        # if have same values in both, then we have 2 scenarios to choose either.
        # 
        # Either choose the 
        cand = len(nums) - odd[-1][0] - (even[-2][0] if len(even)>1 else 0)
        cand1 = len(nums) - even[-1][0] - (odd[-2][0] if len(odd)>1 else 0)
        
        return min(cand, cand1)



       





        
1022