You are given a 0-indexed integer array nums. The array nums is beautiful if:
nums.length is even.nums[i] != nums[i + 1] for all i % 2 == 0.Note that an empty array is considered beautiful.
You can delete any number of elements from nums. When you delete an element, all the elements to the right of the deleted element will be shifted one unit to the left to fill the gap created and all the elements to the left of the deleted element will remain unchanged.
Return the minimum number of elements to delete from nums to make it beautiful.
Example 1:
Input: nums = [1,1,2,3,5] Output: 1 Explanation: You can delete eithernums[0]ornums[1]to makenums= [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to makenumsbeautiful.
Example 2:
Input: nums = [1,1,2,2,3,3] Output: 2 Explanation: You can deletenums[0]andnums[5]to make nums = [1,2,2,3] which is beautiful. It can be proven you need at least 2 deletions to make nums beautiful.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 105Problem Overview: You receive an integer array and must delete the minimum number of elements so the resulting array becomes beautiful. A beautiful array requires that for every even index i, the condition nums[i] != nums[i+1] holds. After all deletions, the final array length must also be even.
The challenge is that deletions shift indices. Removing an element changes which indices are even, so you cannot simply compare neighbors once and be done. The solution must simulate the resulting array while deciding which elements to keep or discard.
Approach 1: Two-Pointer Technique (Greedy) (Time: O(n), Space: O(1))
This approach scans the array once while tracking the length of the array that would remain after deletions. Think of it as constructing the valid array on the fly. When the current position in the simulated array is even, you must ensure the next kept value differs from the previous element. If two consecutive values are equal at an even position, delete one of them (increment deletion count) and continue scanning. Otherwise, keep the element and extend the valid sequence.
The key insight is that you only care about equality when the constructed index is even. Odd positions have no restriction. After the scan, if the resulting length is odd, delete one more element to ensure the final length is even. This greedy strategy works because keeping the earliest valid pair always preserves maximum flexibility later. This method uses simple iteration over the array and is the optimal interview solution.
Approach 2: Selective Removal and Reposition (Stack-like Construction) (Time: O(n), Space: O(n))
Another way to reason about the problem is to explicitly build the resulting array using a structure similar to a stack or dynamic list. Iterate through the input and append elements to the result unless doing so would violate the beautiful condition. If the current result size is even and the last appended value equals the incoming value, skip the element instead of pushing it. Otherwise append it normally.
This approach makes the constraint easier to visualize because the partially built array always remains valid. The tradeoff is extra memory to store the temporary array. Conceptually this is still a greedy strategy: each decision keeps the earliest valid configuration while minimizing deletions.
Recommended for interviews: The two-pointer greedy solution is the expected answer. It runs in linear time with constant space and demonstrates that you understand how index shifts affect constraints after deletions. Building a temporary array first shows correct reasoning but does not achieve the optimal space complexity.
The goal is to traverse the array and whenever we find two consecutive elements at even index positions that are equal, we delete one of these elements. We can do this in a single pass using a two-pointer technique:
The function uses integers for array access and size. It iterates through the array with a `for` loop. If conditions for `beautiful` are met, it writes the number in place. Finally, it completes by ensuring an even size and returns the number of deletions.
Time Complexity: O(n) because we traverse the array once.
Space Complexity: O(1) as no additional space is used apart from fixed variables.
Here, instead of shifting elements to left positions progressively, we'll keep track of valid pairs directly:
Although somewhat similar, this uses an auxiliary list to track and collect pairs rather implicitly shifting from prior array locations.
This approach checks similar conditions for consecutive pairs but accumulates a `validSize` counter instead of shifting array contents, representing a final count of elements in a beautiful array.
Time Complexity: O(n) for a single iteration.
Space Complexity: O(1) with no additional memory allocations beyond basic variables.
According to the problem description, we know that a beautiful array has an even number of elements, and if we divide every two adjacent elements in this array into a group, then the two elements in each group are not equal. This means that the elements within a group cannot be repeated, but the elements between groups can be repeated.
Therefore, we consider traversing the array from left to right. As long as we encounter two adjacent elements that are equal, we delete one of them, that is, the deletion count increases by one; otherwise, we can keep these two elements.
Finally, we check whether the length of the array after deletion is even. If not, it means that we need to delete one more element to make the final array length even.
The time complexity is O(n), where n is the length of the array. We only need to traverse the array once. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Two-Pointer Technique | Time Complexity: O(n) because we traverse the array once. |
| Selective Removal And Reposition | Time Complexity: O(n) for a single iteration. |
| Greedy | — |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Two-Pointer Greedy | O(n) | O(1) | Best general solution. Single pass with constant memory. |
| Selective Removal and Reposition (Build Result Array) | O(n) | O(n) | Useful when simulating the resulting array explicitly for clarity. |
2216. Minimum Deletions to Make Array Beautiful || Leetcode Weekly Contest 286 || Leetcode 2216 • Bro Coders • 3,122 views views
Watch 9 more video solutions →Practice Minimum Deletions to Make Array Beautiful with our built-in code editor and test cases.
Practice on FleetCode