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
iin the range[0, n - 1], replacenums[i]with eithernums[i],nums[(i - 1 + n) % n], ornums[(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
1We can equalize the array in 1 second by replacing values so that all entries become 2, and it can be proven that 1 second is the minimum needed.
Example 2
Input
nums = [2,1,3,3,2]Output
2We can equalize the array in 2 seconds by first making the array [2,3,3,3,3] and then [3,3,3,3,3], and it can be proven that 2 seconds is the minimum needed.
Constraints
- 1 <= n == nums.length <= 10^5
- 1 <= nums[i] <= 10^9