Skip to main content

Minimum Deletions to Make Array Beautiful - Solution & Explanation

MediumArrayStackGreedy23 min readAsked at: Microsoft
Practice this problem

Problem Statement

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 either nums[0] or nums[1] to make nums = [1,2,3,5] which is beautiful. It can be proven you need at least 1 deletion to make nums beautiful.

Example 2:

Input: nums = [1,1,2,2,3,3]
Output: 2
Explanation: You can delete nums[0] and nums[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 <= 105
  • 0 <= nums[i] <= 105

Approach Overview

Problem 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.

Approach 1: Two-Pointer Technique

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:

  • Create a 'write' pointer initialized to 0 to place elements that are verified to be correct for a beautiful array.
  • Iterate the 'read' pointer through the array, and whenever a pair `(nums[read], nums[read+1])` is valid (i != i+1), 'write' these numbers to the start of the array sequentially and increase the 'write' index.
  • The post-loop array size may be odd, so make sure it is even by potentially removing one extra element.

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) because we traverse the array once.
Space Complexity: O(1) as no additional space is used apart from fixed variables.

Try this approach in the editor →

Approach 2: Selective Removal And Reposition

Here, instead of shifting elements to left positions progressively, we'll keep track of valid pairs directly:

  • Loop over the array and store only valid non-adjacent pairs in an auxiliary array.
  • This can keep a count of these, and ultimately removing the elements not part of any valid pair is equivalent to subtraction from an initial number count.

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) for a single iteration.
Space Complexity: O(1) with no additional memory allocations beyond basic variables.

Try this approach in the editor →

Approach 3: Greedy

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).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Approach 4: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Two-Pointer Technique

Time Complexity: O(n) because we traverse the array once.
Space Complexity: O(1) as no additional space is used apart from fixed variables.

Selective Removal And Reposition

Time Complexity: O(n) for a single iteration.
Space Complexity: O(1) with no additional memory allocations beyond basic variables.

Greedy
Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Two-Pointer GreedyO(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.

Video Solution

2216. Minimum Deletions to Make Array Beautiful || Leetcode Weekly Contest 286 || Leetcode 2216Bro Coders3,139 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Deletions to Make Array Beautiful easy or hard?
The problem is rated Medium on LeetCode with an acceptance rate around 50%. The logic is simple once you realize that deletions change index parity, but identifying the greedy condition during interviews can take some practice.
Minimum Deletions to Make Array Beautiful Python/Java solution
Implement the greedy scan with a counter for deletions and a variable tracking the current valid length. The same logic translates directly to Python, Java, C++, JavaScript, and C#. Each language performs a single pass through the array and updates the deletion count when equal elements appear at an even position.
How to solve Minimum Deletions to Make Array Beautiful in O(n)?
Traverse the array while maintaining the length of the valid sequence built so far. If the current index of the constructed array is even and the next value equals the previous kept value, count a deletion instead of adding it. Otherwise include the element and continue. After traversal, remove one more element if the resulting length is odd.
What is the best approach for Minimum Deletions to Make Array Beautiful?
The best approach is a greedy two-pointer technique that scans the array once while tracking the effective index of the resulting array. When the current simulated index is even and two adjacent values are equal, one element must be deleted. This method runs in O(n) time with O(1) extra space.
Is Minimum Deletions to Make Array Beautiful asked at Google/Amazon/Meta?
Greedy array problems similar to this appear frequently in interviews at companies like Amazon, Google, and Meta. The problem tests understanding of array manipulation, greedy decisions, and how deletions affect index positions.
What data structure is used in Minimum Deletions to Make Array Beautiful?
The optimal solution mainly uses simple array traversal with counters, making it a greedy algorithm on arrays. An alternative implementation builds the resulting array using a list or stack-like structure to enforce the constraint while iterating.
What is the time complexity of Minimum Deletions to Make Array Beautiful?
The optimal solution runs in O(n) time because the array is processed in a single pass. Each element is examined once and either kept or counted as a deletion. Space complexity is O(1) for the two-pointer approach since no additional data structures are required.

Ready to solve this problem?

Practice Minimum Deletions to Make Array Beautiful with our built-in code editor and test cases.

Practice on FleetCode