Algorithms
Medium
2808
2920
Minimum Seconds to Equalize a Circular Array
minimum-seconds-to-equalize-a-circular-array
"""
You are given a 0-indexed array nums containing n integers.

At each second, you perform the following operation on the array:

 * For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n].

Note that all the elements get replaced simultaneously.

Return the minimum number of seconds needed to make all elements in the array nums equal.

Example 1:

Input: nums = [1,2,1,2]
Output: 1
Explanation: We can equalize the array in 1 second in the following way:
- At 1st 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.

Example 2:

Input: nums = [2,1,3,3,2]
Output: 2
Explanation: We can equalize the array in 2 seconds in the following way:
- At 1st 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 2nd 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.

Example 3:

Input: nums = [5,5,5,5]
Output: 0
Explanation: We don't need to perform any operations as all elements in the initial array are the same.


Constraints:

 * 1 <= n == nums.length <= 105
 * 1 <= nums[i] <= 109
"""
class Solution:
    def minimumSeconds(self, nums: List[int]) -> int:
        
You are given a 0-indexed array nums containing n integers.

At each second, you perform the following operation on the array:

 * For every index i in the range [0, n - 1], replace nums[i] with either nums[i], nums[(i - 1 + n) % n], or nums[(i + 1) % n].

Note that all the elements get replaced simultaneously.

Return the minimum number of seconds needed to make all elements in the array nums equal.

Example 1:

Input: nums = [1,2,1,2]
Output: 1
Explanation: We can equalize the array in 1 second in the following way:
- At 1st 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.

Example 2:

Input: nums = [2,1,3,3,2]
Output: 2
Explanation: We can equalize the array in 2 seconds in the following way:
- At 1st 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 2nd 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.

Example 3:

Input: nums = [5,5,5,5]
Output: 0
Explanation: We don't need to perform any operations as all elements in the initial array are the same.


Constraints:

 * 1 <= n == nums.length <= 105
 * 1 <= nums[i] <= 109


Please complete the code below to solve above prblem:
```python
class Solution:
    def minimumSeconds(self, nums: List[int]) -> int:
        
```

my_solution = Solution()

test_input = { "nums": [1,2,1,2] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [2,1,3,3,2] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [5,5,5,5] }
assert my_solution.minimumSeconds(**test_input) == 0

test_input = { "nums": [4,18] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [11,7] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [14,2] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [14,9] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [20,1] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [17,15] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [11,13] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [7,13] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [18,17] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [15,17] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [13,8] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [12,16] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [12,8] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [18,16] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [16,16] }
assert my_solution.minimumSeconds(**test_input) == 0

test_input = { "nums": [6,12] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [9,6] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [2,17] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [7,5] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [15,6] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [3,14] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [6,16] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [3,16] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [11,9] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [4,15] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [16,2] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [12,7] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [7,9] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [7,3] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [8,4] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [4,11] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [6,15] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [9,7] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [19,4] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [17,6] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [18,10] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [8,3] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [11,4,10] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [5,3,13] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [2,1,15] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [17,13,9] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [2,15,12] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [16,12,13] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [1,12,19] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [4,3,3] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [15,11,3] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [15,18,16] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [20,6,7] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [9,4,7] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [20,11,6] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [11,8,16] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [1,4,12] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [18,12,19] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [8,11,5] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [6,10,6] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [3,10,16] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [15,6,15] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [9,5,1] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [15,15,18] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [3,16,13] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [9,3,6] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [4,14,9] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [15,2,20] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [13,7,19] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [19,19,12] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [8,6,1] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [19,10,19] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [6,9,17] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [3,19,8,12] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [6,9,14,16] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [19,12,14,16] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [12,20,13,19] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [12,14,13,8] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [8,13,3,3] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [5,11,7,15] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [7,11,7,5] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [1,8,7,15] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [13,1,19,3] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [19,15,19,1] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [7,13,20,9] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [1,18,16,6] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [9,16,6,13] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [11,9,5,1] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [6,3,15,10] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [12,1,9,19] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [15,14,14,19] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [2,5,13,5] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [4,8,8,13] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [3,9,10,13] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [7,17,11,8] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [4,5,15,11] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [11,15,19,12] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [8,20,5,10] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [5,3,5,17] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [14,19,8,8] }
assert my_solution.minimumSeconds(**test_input) == 1

test_input = { "nums": [16,20,4,13] }
assert my_solution.minimumSeconds(**test_input) == 2

test_input = { "nums": [17,16,2,16] }
assert my_solution.minimumSeconds(**test_input) == 1