LeetCode Problem

How to Solve Reduce Array Size to The Half

The goal is to reduce an array’s size to at least half by removing the fewest number of integers. Focus on scanning the array and counting integer frequencies. Sorting the frequencies and picking the most common elements minimizes the number of removals needed.

GhostInterview Help

Need help with Reduce Array Size to The Half without spending extra time grinding it?

GhostInterview can read Reduce Array Size to The Half from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1338Array scanning plus hash lookupReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The goal is to reduce an array’s size to at least half by removing the fewest number of integers. Focus on scanning the array and counting integer frequencies. Sorting the frequencies and picking the most common elements minimizes the number of removals needed.

Problem Statement

You are given an array of integers, arr, and need to remove a set of integers from it. The objective is to remove at least half of the integers in the array by removing all occurrences of selected integers.

Return the minimum number of integers that need to be removed so that at least half of the array is gone. For instance, given arr = [3,3,3,3,5,5,5,2,2,7], removing the integers {3,7} results in an array that is reduced to half its original size.

Examples

Example 1

Input: arr = [3,3,3,3,5,5,5,2,2,7]

Output: 2

Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array). Possible sets of size 2 are {3,5},{3,2},{5,2}. Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has a size greater than half of the size of the old array.

Example 2

Input: arr = [7,7,7,7,7,7]

Output: 1

The only possible set you can choose is {7}. This will make the new array empty.

Constraints

  • 2 <= arr.length <= 105
  • arr.length is even.
  • 1 <= arr[i] <= 105

Solution Approach

Count frequencies of integers

Start by counting the frequency of each integer in the array using a hash table. This helps determine the most frequent integers, which should be prioritized for removal to minimize the number of removed elements.

Sort frequencies and remove most frequent elements

Sort the frequencies in descending order, then iteratively remove the most frequent integers. This greedy approach ensures that each removal contributes the maximum possible reduction in the array size.

Stop once half of the array is removed

Keep track of the cumulative number of elements removed and stop once at least half of the array is gone. The number of distinct integers removed at this point is the answer.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on the sorting step, which is O(n log n), where n is the number of elements in the array. Counting frequencies takes O(n) time. Therefore, the overall time complexity is O(n log n). Space complexity is O(n) due to the frequency hash table.

What Interviewers Usually Probe

  • Look for an understanding of greedy algorithms and how to apply them efficiently.
  • Check if the candidate can prioritize based on frequency counts for optimization.
  • Evaluate if the candidate avoids unnecessary complexity by focusing on sorting and frequency analysis.

Common Pitfalls or Variants

Common pitfalls

  • Not sorting the frequency list and blindly removing random elements can lead to suboptimal solutions.
  • Failing to account for cases where only one integer needs removal.
  • Over-complicating the approach with unnecessary data structures or logic.

Follow-up variants

  • What if the array has no duplicates? This changes the dynamic of removal.
  • What if you need to remove exactly half of the array, not just at least half?
  • Consider situations where integers in the array are very large but their counts are small.

How GhostInterview Helps

  • GhostInterview helps you simulate optimal strategies like counting frequencies and applying greedy removal in real-time.
  • The tool ensures you can practice the key steps such as frequency counting and sorting to match the problem's pattern.
  • It highlights the critical trade-off between time complexity and clarity, reinforcing the efficient sorting-based approach.

Topic Pages

FAQ

What is the core pattern for solving the Reduce Array Size to The Half problem?

The core pattern is array scanning plus hash lookup to count frequencies and then greedily removing the most frequent integers.

How do you reduce the array size to half in the least number of removals?

By selecting integers with the highest frequency and removing them, you can efficiently reduce the array size to at least half.

Can this problem be solved with a sliding window approach?

No, a sliding window is not applicable here. The problem requires counting frequencies and removing the most frequent elements directly.

What is the time complexity of this problem?

The time complexity is O(n log n) due to sorting the frequency counts, where n is the length of the array.

What should I focus on to solve this problem efficiently?

Focus on efficiently counting the frequencies of integers, sorting the counts, and removing the most frequent ones until half of the array is removed.

GhostInterview Solver

Need direct help with Reduce Array Size to The Half instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Reduce Array Size to The Half from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.