DDSA Solutions

3634. Minimum Removals to Balance Array

Time: O(n)
Space: O(n)

Problem Overview

Minimum Removals to Balance Array (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Greedy pattern in coding interviews. Precompute prefix min-max and suffix min-max; find min removals where left max < right min.

A full step-by-step explanation is being added. See the study guide for pattern-based practice.

Approach

Precompute prefix min-max and suffix min-max; find min removals where left max < right min.

Related patterns: Array, Greedy

3634.cs
C#
// Approach: Precompute prefix min-max and suffix min-max; find min removals where left max < right min.
// Time: O(n) Space: O(n)

public class Solution
{
    public int MinRemoval(int[] nums, int k)
    {
        Array.Sort(nums);
        int cnt = 0;
        int n = nums.Length;
        for (int i = 0; i < n; ++i)
        {
            int j = n;
            if ((long)nums[i] * k <= nums[n - 1])
            {
                j = Array.BinarySearch(nums, nums[i] * k + 1);
                j = j < 0 ? ~j : j;
            }
            cnt = Math.Max(cnt, j - i);
        }

        return n - cnt;
    }
}
Was this solution helpful?

Related Problems