702925. Remove minimum elements

Medium

Here is the generated Markdown blog post:

Remove Minimum Elements

Slug: remove-minimum-elements4612

Difficulty: Medium

Id: 702925

Topic Tags: Arrays, Data Structures

Company Tags: Amazon

Summary

Given an array of integers and a positive integer k, the task is to find and remove all elements that are equal to the minimum value in each subarray of size k. The array should be modified such that it does not contain any duplicate values.

Detailed Explanation

To solve this problem, we can iterate through the array with a sliding window of size k. For each window, we find the minimum element and remove all occurrences of this element from the original array. We continue this process until the end of the array is reached.

Here's a step-by-step breakdown of the solution:

  1. Initialize an empty array result to store the modified elements.
  2. Iterate through the input array with a sliding window of size k.
  3. For each window, find the minimum element using a simple loop or a more efficient data structure like a min-heap.
  4. Remove all occurrences of the minimum element from the original array.
  5. Add the modified elements to the result array.
  6. Repeat steps 2-5 until the end of the array is reached.
  7. Return the modified result array.

Time complexity: O(n), where n is the length of the input array. Space complexity: O(n), as we need to store the modified elements in the result array.

Optimized Solutions

Java

View on GeeksforGeeks
public int[] removeMinimumElements(int[] arr, int k) {
    int resultSize = 0;
    for (int i = 0; i < arr.length; i++) {
        if (i >= k - 1) {
            int min = Arrays.stream(arr).min().getAsInt();
            while (arr[i] == min) {
                arr = Arrays.copyOfRange(arr, 0, i);
                resultSize++;
                i--;
            }
        }
    }
    return Arrays.copyOfRange(arr, 0, arr.length - resultSize);
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.