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

<p>At each second, you perform the following operation on the array:</p>

<ul>
	<li>For every index <code>i</code> in the range <code>[0, n - 1]</code>, replace <code>nums[i]</code> with either <code>nums[i]</code>, <code>nums[(i - 1 + n) % n]</code>, or <code>nums[(i + 1) % n]</code>.</li>
</ul>

<p><strong>Note</strong> that all the elements get replaced simultaneously.</p>

<p>Return <em>the <strong>minimum</strong> number of seconds needed to make all elements in the array</em> <code>nums</code> <em>equal</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,1,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can equalize the array in 1 second in the following way:
- At 1<sup>st</sup> second, replace values at each index with [nums[3],nums[1],nums[3],nums[3]]. After replacement, nums = [2,2,2,2].
It can be proven that 1 second is the minimum amount of seconds needed for equalizing the array.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,1,3,3,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can equalize the array in 2 seconds in the following way:
- At 1<sup>st</sup> second, replace values at each index with [nums[0],nums[2],nums[2],nums[2],nums[3]]. After replacement, nums = [2,3,3,3,3].
- At 2<sup>nd</sup> second, replace values at each index with [nums[1],nums[1],nums[2],nums[3],nums[4]]. After replacement, nums = [3,3,3,3,3].
It can be proven that 2 seconds is the minimum amount of seconds needed for equalizing the array.
</pre>

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

<pre>
<strong>Input:</strong> nums = [5,5,5,5]
<strong>Output:</strong> 0
<strong>Explanation:</strong> We don&#39;t need to perform any operations as all elements in the initial array are the same.
</pre>

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

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

class Solution:
    def minimumSeconds(self, nums: List[int]) -> int:
        l = len(nums)

        mapper = defaultdict(list)

        for i, num in enumerate(nums):
            mapper[num].append(i)
        
        maxi = float("inf")
        print(mapper)
        for k in mapper:
            cur_max = 0
            length = len(mapper[k])

            if length == 1:
                maxi = min(maxi, l - 1)
                continue
            
            for i in range(length):
                cur = mapper[k][i]
                prev = mapper[k][(i - 1) % length]
                next = mapper[k][(i + 1) % length]

                prev_dist, next_dist = 0, 0
                if prev > cur:
                    prev_dist = l - 2 - (prev - cur - 1)
                else:
                    prev_dist = cur - prev - 1

                if next < cur:
                    next_dist = l - 2 - (cur - next - 1)
                else:
                    next_dist = next - cur - 1

            
                cur_max = max(cur_max, prev_dist, next_dist)
                # print(k, prev_dist, next_dist)
            # print(maxi, cur_max)
            # print(k, cur_max)
            maxi = min(maxi, cur_max)
        
        # print(maxi)
        return ceil(maxi / 2) if maxi != float('inf') else 0
48500