Minimum Number of Operations to Make Elements in Array Distinct (LeetCode 3396): Solution and Explanation
Each operation removes the first three elements of the array. Return the fewest operations after which the remaining elements are all distinct.
Intuition
Removing from the front means the array that survives is always a suffix. After k operations it is the suffix that starts at index 3k.
So the question becomes: what is the leftmost index where a distinct suffix can start? Everything before that point has to go.
Scan from the right end and keep a set of the values you have passed. As long as each new value is not in the set, the suffix is still distinct.
The first index i whose value is already in the set is the problem. It has to be removed, and it sits in block i // 3, so you need i // 3 + 1 operations.
Approach: Backward scan with a set
- Create an empty set
seen. - Walk
ifrom the last index down to 0. - If
nums[i]is already inseen, returni // 3 + 1. - Otherwise add
nums[i]toseen. - If the loop ends, every element is distinct, so return 0.
Step-by-step example
nums = [1,2,3,4,2,3,3,5,7]
- i = 8, 7, 6: the values 7, 5 and 3 are new, so seen = {7, 5, 3}.
- i = 5: the value 3 is already in seen, so the suffix starting at index 5 has a repeat.
- Index 5 is in block 1 (indices 3 to 5). Removing blocks 0 and 1 takes 5 // 3 + 1 = 2 operations.
- The remaining array is [3,5,7], which is distinct. Return 2.
Code: Python, Java, C++
1class Solution:
2 def minimumOperations(self, nums: List[int]) -> int:
3 seen = set()
4 for i in range(len(nums) - 1, -1, -1):
5 if nums[i] in seen:
6 return i // 3 + 1
7 seen.add(nums[i])
8 return 01class Solution {
2 public int minimumOperations(int[] nums) {
3 Set<Integer> seen = new HashSet<>();
4 for (int i = nums.length - 1; i >= 0; i--) {
5 if (seen.contains(nums[i])) {
6 return i / 3 + 1;
7 }
8 seen.add(nums[i]);
9 }
10 return 0;
11 }
12}1class Solution {
2public:
3 int minimumOperations(vector<int>& nums) {
4 unordered_set<int> seen;
5 for (int i = (int)nums.size() - 1; i >= 0; i--) {
6 if (seen.count(nums[i])) {
7 return i / 3 + 1;
8 }
9 seen.insert(nums[i]);
10 }
11 return 0;
12 }
13};Time and space complexity
O(n)
Each element is visited at most once and set lookups are constant time on average.
O(n)
The set can hold up to n values.
Other approaches
Simulate the operations (time O(n^2), space O(n))
Repeatedly check whether the current suffix is distinct, and remove three elements if not. Each check costs O(n), so the whole thing is O(n^2), which is fine for n = 100 but does more work than needed.
Counting array (time O(n), space O(1))
Since values are at most 100, a boolean array of size 101 can replace the hash set. The logic is identical.
Common mistakes
- Scanning from the left. The surviving part is a suffix, so the scan has to start at the right end to find where the distinct suffix stops.
- Returning i // 3 instead of i // 3 + 1. Block numbers start at 0, but removing block 0 already costs one operation.
- Forgetting the case with no repeats. Then the answer is 0, not 1.
- Thinking a short leftover must be removed in full. An empty array counts as distinct, so the formula already covers a last partial block.
Frequently asked questions
Why is the answer i // 3 + 1?
Index i belongs to block i // 3 (indices 0 to 2 are block 0, 3 to 5 are block 1, and so on). Removing that block means removing every block before it too, which is i // 3 + 1 operations.
Why scan from the right instead of the left?
Operations only remove from the front, so the remaining array is always a suffix. Scanning from the right grows the longest distinct suffix one element at a time and stops at the first repeat.
What if the whole array gets removed?
That is allowed. An empty array counts as distinct, and the formula gives exactly the number of operations needed to clear it.