2170. Minimum Operations to Make the Array Alternating LeetCode Solution

In this guide, you will get 2170. Minimum Operations to Make the Array Alternating LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Make the Array Alternating 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 Operations to Make the Array Alternating solution in C++
  4. Minimum Operations to Make the Array Alternating solution in Java
  5. Minimum Operations to Make the Array Alternating solution in Python
  6. Additional Resources
2170. Minimum Operations to Make the Array Alternating LeetCode Solution image

Problem Statement of Minimum Operations to Make the Array Alternating

You are given a 0-indexed array nums consisting of n positive integers.
The array nums is called alternating if:

nums[i – 2] == nums[i], where 2 <= i <= n – 1.
nums[i – 1] != nums[i], where 1 <= i <= n – 1.

In one operation, you can choose an index i and change nums[i] into any positive integer.
Return the minimum number of operations required to make the array alternating.

Example 1:

Input: nums = [3,1,3,2,4,3]
Output: 3
Explanation:
One way to make the array alternating is by converting it to [3,1,3,1,3,1].
The number of operations required in this case is 3.
It can be proven that it is not possible to make the array alternating in less than 3 operations.

Example 2:

Input: nums = [1,2,2,2,2]
Output: 2
Explanation:
One way to make the array alternating is by converting it to [1,2,1,2,1].
The number of operations required in this case is 2.
Note that the array cannot be converted to [2,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.

See also  3477. Fruits Into Baskets II LeetCode Solution

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 105

Complexity Analysis

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

2170. Minimum Operations to Make the Array Alternating LeetCode Solution in C++

struct T {
  unordered_map<int, int> count;
  int mx = 0;
  int secondMax = 0;
  int maxFreq = 0;
  int secondMaxFreq = 0;
};

class Solution {
 public:
  int minimumOperations(vector<int>& nums) {
    // 0 := odd indices, 1 := even indices
    vector<T> ts(2);

    for (int i = 0; i < nums.size(); ++i) {
      T& t = ts[i % 2];
      const int freq = ++t.count[nums[i]];
      if (freq > t.maxFreq) {
        t.maxFreq = freq;
        t.mx = nums[i];
      } else if (freq > t.secondMaxFreq) {
        t.secondMaxFreq = freq;
        t.secondMax = nums[i];
      }
    }

    if (ts[0].mx == ts[1].mx)
      return nums.size() - max(ts[0].maxFreq + ts[1].secondMaxFreq,
                               ts[1].maxFreq + ts[0].secondMaxFreq);
    return nums.size() - (ts[0].maxFreq + ts[1].maxFreq);
  }
};
/* code provided by PROGIEZ */

2170. Minimum Operations to Make the Array Alternating LeetCode Solution in Java

class T {
  public Map<Integer, Integer> count = new HashMap<>();
  public int mx = 0;
  public int secondMax = 0;
  public int maxFreq = 0;
  public int secondMaxFreq = 0;
}

class Solution {
  public int minimumOperations(int[] nums) {
    T[] ts = new T[2];
    ts[0] = new T();
    ts[1] = new T();

    for (int i = 0; i < nums.length; ++i) {
      T t = ts[i % 2];
      t.count.merge(nums[i], 1, Integer::sum);
      final int freq = t.count.get(nums[i]);
      if (freq > t.maxFreq) {
        t.maxFreq = freq;
        t.mx = nums[i];
      } else if (freq > t.secondMaxFreq) {
        t.secondMaxFreq = freq;
        t.secondMax = nums[i];
      }
    }

    if (ts[0].mx == ts[1].mx)
      return nums.length -
          Math.max(ts[0].maxFreq + ts[1].secondMaxFreq, ts[1].maxFreq + ts[0].secondMaxFreq);
    return nums.length - (ts[0].maxFreq + ts[1].maxFreq);
  }
}
// code provided by PROGIEZ

2170. Minimum Operations to Make the Array Alternating LeetCode Solution in Python

class T:
  def __init__(self):
    self.count = collections.Counter()
    self.mx = 0
    self.secondMax = 0
    self.maxFreq = 0
    self.secondMaxFreq = 0


class Solution:
  def minimumOperations(self, nums: list[int]) -> int:
    # 0 := odd indices, 1 := even indices
    ts = [T() for _ in range(2)]

    for i, num in enumerate(nums):
      t = ts[i % 2]
      t.count[num] += 1
      freq = t.count[num]
      if freq > t.maxFreq:
        t.maxFreq = freq
        t.mx = num
      elif freq > t.secondMaxFreq:
        t.secondMaxFreq = freq
        t.secondMax = num

    if ts[0].mx == ts[1].mx:
      return len(nums) - max(ts[0].maxFreq + ts[1].secondMaxFreq,
                             ts[1].maxFreq + ts[0].secondMaxFreq)
    return len(nums) - (ts[0].maxFreq + ts[1].maxFreq)
# code by PROGIEZ

Additional Resources

See also  178. Rank Scores LeetCode Solution

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