The Indian Engineer

Problem 3396 Minimum Number of Operations to Make Elements in Array Distinct

Posted on 6 mins

Array Hash-Table

Problem Statement

Link - Problem 3396

Question

You are given an integer array nums. You need to ensure that the elements in the array are distinct. To achieve this, you can perform the following operation any number of times:

Note that an empty array is considered to have distinct elements. Return the minimum number of operations needed to make the elements in the array distinct.

Example 1:

Input: nums = [1,2,3,4,2,3,3,5,7]

Output: 2

Explanation:

  • In the first operation, the first 3 elements are removed, resulting in the array [4, 2, 3, 3, 5, 7].
  • In the second operation, the next 3 elements are removed, resulting in the array [3, 5, 7], which has distinct elements.

Therefore, the answer is 2.

Example 2:

Input: nums = [4,5,6,4,4]

Output: 2

Explanation:

  • In the first operation, the first 3 elements are removed, resulting in the array [4, 4].
  • In the second operation, all remaining elements are removed, resulting in an empty array.

Therefore, the answer is 2.

Example 3:

Input: nums = [6,7,8,9]

Output: 0

Explanation:

The array already contains distinct elements. Therefore, the answer is 0.

Constraints:

Solution

class Solution {
public:
    int minimumOperations(vector<int>& nums) {
        vector<bool> seen(101);
        for (int i = nums.size() - 1; i >= 0; i--) {
            if (seen[nums[i]]) {
                return i / 3 + 1;
            }
            seen[nums[i]] = true;
        }
        return 0;
    }
};

Complexity Analysis

| Algorithm     | Time Complexity | Space Complexity |
| ------------- | --------------- | ---------------- |
| Linear Search | O(n)            | O(1)             |

Explanation

Intial Thoughts

To approach this problem, we should first consider the constraints and the allowed operation. We can remove 3 elements from the beginning of the array at a time. Thinking about the problem in reverse might be helpful, as we can start from the end and work our way backward, keeping track of the elements we’ve seen so far. We should also note that an empty array is considered to have distinct elements, which could be a base case for our solution. Breaking down the problem into smaller sub-problems and considering the worst-case scenario could provide more insight. Lastly, thinking about how to utilize the given constraints to minimize the number of operations will be key.

Intuitive Analysis

Intuitively, we can think of this problem as trying to find the minimum number of ‘blocks’ of 3 elements that need to be removed from the beginning of the array to make the remaining elements distinct. We can use a ‘seen’ array to keep track of the elements we’ve encountered so far, similar to how we might use a map to keep track of unique elements in other problems. Starting from the end of the array and moving backward allows us to take advantage of the fact that we can remove elements in ‘blocks’ of 3. By using this approach, we can ensure that we’re removing the minimum number of elements necessary to make the remaining elements distinct. Additionally, thinking about the problem in terms of ‘removing blocks’ rather than individual elements can help simplify the solution and make it more efficient.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Easy difficulty.

Hints

The constraints are small. Try brute force.


Similar Questions:

Title URL Difficulty
Minimum Increment to Make Array Unique https://leetcode.com/problems/minimum-increment-to-make-array-unique Medium