FastPrep🐘 Keep Them Apart πŸ¦™
Problems/🐘 Keep Them Apart πŸ¦™

🐘 Keep Them Apart πŸ¦™

QRT logoQRT● MediumNEW GRADOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given an array A of length n and an integer d >= 1. You may delete elements from the array from any position.

After deletions, consider the original indices of the remaining elements, i.e. their position in the input before any deletions. The array is valid if, for every value x, any two kept occurrences of x that appear at original positions i < j satisfy j - i >= d.

Your task is to compute the minimum number of deletions required to make the array valid.

Note: Distances are measured using the original indices from the input array, not the compressed indices after deletion.

Function

minimumDeletions(A: int[], d: int) β†’ int

Examples

Example 1

A = [1, 2, 2, 1]d = 3return = 1

When x = 1, we have indices 1 and 4; we can keep both because 4 - 1 >= 3.

When x = 2, we have indices 2 and 3; we need to delete at least one element.

Therefore, the minimum number of deletions is 1.

Constraints

  • n <= 10^5, d <= 10^5, and 0 < A[i] <= 10^9 for all test cases.
  • Subtask A: n <= 10, d <= 10, and 0 < A[i] <= 10.
  • Subtask B: n <= 100, d <= 100, and 0 < A[i] <= 10.
  • Subtask C: n <= 10^5, d <= 100, and 0 < A[i] <= 10^9.
  • Subtask D: n <= 10^5, d <= 10^5, and 0 < A[i] <= 10^9.

More QRT Online Assessment questions

  • Array NullificationOA Β· Seen Jul 2026

Similar questions at other companies

  • Minimum Rounds to Complete All TasksInfosys Β· OAInfosys interview questions
  • Minimum Distinct Prefix CostChubb Β· OAChubb interview questions
  • Minimum Distinct Prefix CostGoldman Sachs Β· OAGoldman Sachs interview questions
  • Rearrange Songs by AuthorZomato / Eternal Β· OAZomato / Eternal interview questions
  • Minimum Operations to Make Array ValidAmazon Β· OA
  • Maximum MEXAtlassian Β· OAAtlassian interview questions
  • Minimum Labels after Removing BoxesExpedia Β· OAExpedia interview questions
  • Minimize Distinct Item Types After RemovalsRoblox Β· OARoblox interview questions
  • Minimum Operations to Make a SubsequenceGoogle Β· OAGoogle interview questions
  • Maximum Distinct Values After SwapsAkuna Capital Β· OAAkuna Capital interview questions
See QRT hiring insights
Saved
class Solution {
    public int minimumDeletions(int[] A, int d) {
      // write your code here
    }
}
Checking account…