Problem Β· Array

🐘 Keep Them Apart πŸ¦™

Learn this problem
● MediumQube Research & TechnologiesNEW GRADOA

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 Qube Research & Technologies problems

drafts saved locally
public int minimumDeletions(int[] A, int d) {
  // write your code here
}
A[1, 2, 2, 1]
d3
expected1
checking account