2808. Minimum Seconds to Equalize a Circular Array LeetCode Solution

In this guide, you will get 2808. Minimum Seconds to Equalize a Circular Array LeetCode Solution with the best time and space complexity. The solution to Minimum Seconds to Equalize a Circular Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Seconds to Equalize a Circular Array solution in C++
  4. Minimum Seconds to Equalize a Circular Array solution in Java
  5. Minimum Seconds to Equalize a Circular Array solution in Python
  6. Additional Resources
2808. Minimum Seconds to Equalize a Circular Array LeetCode Solution image

Problem Statement of 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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2808. Minimum Seconds to Equalize a Circular Array LeetCode Solution in C++

class Solution {
 public:
  int minimumSeconds(vector<int>& nums) {
    const int n = nums.size();
    int ans = n;
    unordered_map<int, vector<int>> numToIndices;

    for (int i = 0; i < n; ++i)
      numToIndices[nums[i]].push_back(i);

    for (const auto& [_, indices] : numToIndices) {
      int seconds = getSeconds(indices.front() + n, indices.back());
      for (int i = 1; i < indices.size(); ++i)
        seconds = max(seconds, getSeconds(indices[i], indices[i - 1]));
      ans = min(ans, seconds);
    }

    return ans;
  }

 private:
  // Returns the number of seconds required to make nums[i..j] the same.
  int getSeconds(int i, int j) {
    return (i - j) / 2;
  }
};
/* code provided by PROGIEZ */

2808. Minimum Seconds to Equalize a Circular Array LeetCode Solution in Java

class Solution {
  public int minimumSeconds(List<Integer> nums) {
    int n = nums.size();
    int ans = n;
    Map<Integer, List<Integer>> numToIndices = new HashMap<>();

    for (int i = 0; i < n; ++i) {
      numToIndices.putIfAbsent(nums.get(i), new ArrayList<>());
      numToIndices.get(nums.get(i)).add(i);
    }

    for (List<Integer> indices : numToIndices.values()) {
      int seconds = getSeconds(indices.get(0) + n, indices.get(indices.size() - 1));
      for (int i = 1; i < indices.size(); ++i)
        seconds = Math.max(seconds, getSeconds(indices.get(i), indices.get(i - 1)));
      ans = Math.min(ans, seconds);
    }

    return ans;
  }

  // Returns the number of seconds required to make nums[i..j] the same.
  private int getSeconds(int i, int j) {
    return (i - j) / 2;
  }
}
// code provided by PROGIEZ

2808. Minimum Seconds to Equalize a Circular Array LeetCode Solution in Python

class Solution:
  def minimumSeconds(self, nums: list[int]) -> int:
    n = len(nums)
    ans = n
    numToIndices = collections.defaultdict(list)

    for i, num in enumerate(nums):
      numToIndices[num].append(i)

    def getSeconds(i: int, j: int) -> int:
      """Returns the number of seconds required to make nums[i..j] the same."""
      return (i - j) // 2

    for indices in numToIndices.values():
      seconds = getSeconds(indices[0] + n, indices[-1])
      for i in range(1, len(indices)):
        seconds = max(seconds, getSeconds(indices[i], indices[i - 1]))
      ans = min(ans, seconds)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.