Keep Them Apart
Reported by candidates from QRT's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
QRT reported this one in July 2026, and the whole solution hinges on a hash map from value to its list of original indices. If you've got an OA coming, this is the shape to recognize: each distinct value is its own independent problem, and you solve them separately and add up the deletions. The title says it all. Keep equal values at least d apart, measured on original positions. It looks like a deletion puzzle but it's really interval scheduling in disguise. If you blank on the grouping idea during the live assessment, StealthCoder can sit invisibly on your screen as a safety net and hand you the approach.
The problem
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 = 3 return = 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Group indices by value with a hash map. Values never interact, so each group is solved alone. Within one group you have a sorted list of positions and want the maximum number you can keep with every consecutive kept pair at least d apart. Greedy works: keep the first, then keep the next index that is at least d past the last kept one, and skip the rest. Deletions for that group equal its size minus kept count. Sum across groups. The common pitfall is measuring distance on compressed indices after deletion, which the problem explicitly forbids. Another trap is a DP over d, which dies at n and d of 10^5. Greedy is linear after grouping, so Subtask D is fine. Since indices are inserted in order, lists are already sorted and you skip sorting. If the greedy proof slips your mind mid-OA, StealthCoder is the hedge that gets you unstuck.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Keep Them Apart cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass QRT's OA.
QRT reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Keep Them Apart FAQ
What's the trick in Keep Them Apart?+
Split the array by value using a hash map of value to index list. Then greedily keep the earliest index and every next index at least d beyond the last kept one. Deletions are group size minus kept count, summed over all groups.
Why is greedy correct here?+
Keeping the earliest available occurrence leaves the most room for later ones, which is the classic interval scheduling exchange argument. Any optimal set can swap its first element for the earliest index without breaking the gap rule, so taking earliest each time never loses.
What complexity do I need for the large subtasks?+
With n and d up to 10^5, you need roughly O(n) or O(n log n). One pass to build the map and one pass per group totals linear time. Anything quadratic in n or O(n*d) will time out on Subtask C and D.
What's the most common mistake?+
Using positions after deletion instead of original indices. The problem states distances use the input positions. In the example, the two 1s at indices 1 and 4 are fine with d = 3, even if elements between them get removed.
How do I prepare for this in 48 hours?+
Write the group-by-value greedy once from memory and test it on the sample, where A = [1,2,2,1] and d = 3 gives 1. Then check edge cases like d = 1, which needs zero deletions, and all equal values. That covers most of what this OA tests.