Mid/SeniorArrayHash Table

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
Inputnums = [1,2,1,2]
Output1
We 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
Inputnums = [2,1,3,3,2]
Output2
We 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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate