Minimum Seconds to Equalize a Circular Array - Problem
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.
Input & Output
Example 1 — Mixed Values
$
Input:
nums = [1,2,1,3,1]
›
Output:
1
💡 Note:
Value 1 is at positions [0,2,4]. It can spread to adjacent positions in 1 second, covering the entire array fastest.
Example 2 — Two Values
$
Input:
nums = [2,1,1,2]
›
Output:
1
💡 Note:
Both values 1 and 2 are well distributed. Each takes 1 second to spread completely.
Example 3 — All Same
$
Input:
nums = [5,5,5,5]
›
Output:
0
💡 Note:
All elements are already equal, no spreading needed.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code