Minimum Seconds to Equalize a Circular Array - Problem
Minimum Seconds to Equalize a Circular Array

Imagine you have a circular array where elements can "spread" their values to adjacent positions simultaneously. At each second, every element in the array can choose to keep its current value or adopt the value from either of its neighbors (left or right, wrapping around circularly).

๐ŸŽฏ Your Goal: Find the minimum number of seconds needed for all elements to become equal.

The Process:
โ€ข At each second, all elements update simultaneously
โ€ข Each element can choose: its current value, left neighbor's value, or right neighbor's value
โ€ข The array is circular - position 0's left neighbor is position n-1
โ€ข Values "spread" optimally to minimize equalization time

Example: [1,2,1,2] โ†’ After 1 second, all elements can become 1 or 2 โ†’ Answer: 1

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1,2,1,2]
โ€บ Output: 1
๐Ÿ’ก Note: Value 1 is at positions [0,2] and value 2 is at positions [1,3]. Both have maximum gaps of 2, so spreading takes 2/2 = 1 second.
example_2.py โ€” All Same Elements
$ Input: nums = [2,2,2,2]
โ€บ Output: 0
๐Ÿ’ก Note: All elements are already equal, so no time is needed.
example_3.py โ€” Single Element Spread
$ Input: nums = [1,2,2,2,1]
โ€บ Output: 2
๐Ÿ’ก Note: Value 1 at positions [0,4] has gap 1, while value 2 at [1,2,3] has maximum gap 3. Min(1/2, 3/2) = min(0, 1) but since we need ceiling, it's min(1, 2) = 1. Actually, value 1 positions [0,4] have circular gap 1, time=1/2=0.5โ†’1. Value 2 positions [1,2,3] have max gap 3 (from 3 to 1), time=3/2=1.5โ†’2. Answer is min(1,2)=1. Wait, let me recalculate: gap from pos 3 to pos 1 going around is (1+5-3)=3, so max gap for value 2 is 3, time needed is 1 (since it can spread from both 2's). For value 1, gap is 1 (4 to 0 circularly), time needed is 2 (since 1's are far apart). So answer is min(1,2)=1.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • Array elements can be any positive integers
  • Circular array: position 0 is adjacent to position n-1

Visualization

Tap to expand
Circular Wave Propagation1212Red waves (value 1) spread from opposite sidesBlue waves (value 2) spread from opposite sidesBoth need 1 second to fill their gaps โ†’ Answer: 1
Understanding the Visualization
1
Identify Sources
Each unique value has specific source positions in the circular array
2
Calculate Gaps
Find the largest distance between consecutive same values (considering wrap-around)
3
Determine Spread Time
Each gap can be filled from both ends, so time = maxGap / 2
4
Find Optimal
Choose the value with minimum spreading time
Key Takeaway
๐ŸŽฏ Key Insight: The optimal strategy recognizes that values spread bidirectionally in circular arrays, making the time proportional to the largest gap divided by 2.
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
31.7K Views
Medium-High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen