Minimum Operations to Make an Array Continuous
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Amazon OA reported in July 2026 dresses up a sliding window as an array problem. Strip the wording and you're asked to pick a window of n consecutive integers that already holds as many of your values as possible. Everything outside that window gets replaced. It's LeetCode 2009 in a thin costume, and the trick is short once you see it. If you blank under the timer, StealthCoder runs invisibly on your desktop and gives you the solution in real time. Know the shape first, though, so you're not relying on it.
The problem
You are given a non-empty integer array nums of length n. In one operation, you may replace any one element with any integer.
An array is continuous when both conditions hold:
All n elements are unique.
The difference between the maximum and minimum elements is exactly n - 1.
Return the minimum number of replacement operations needed to make nums continuous.
Function
minOperations(nums: int[]) → int
Examples
Example 1
nums = [4,2,5,3]
return = 0
The values are already the unique consecutive set {2,3,4,5}.
Example 2
nums = [1,2,3,5,6]
return = 1
Replace 1 with 4 to obtain the continuous set {2,3,4,5,6}.
Example 3
nums = [1,10,100,1000]
return = 3
At most one existing value can be retained inside any interval containing four consecutive integers, so three elements must be replaced.Reported by candidates. Source: FastPrep
Pattern and pitfall
Reduce it fast. Duplicates are useless, so dedupe and sort. Now any final array is a range [x, x+n-1]. Keep as many distinct existing values inside that range as you can. Answer is n minus the max kept. Use two pointers: for each left index, advance right while sorted[right] <= sorted[left] + n - 1. Track the max window size. The classic pitfall is forgetting to dedupe, which counts repeated values as kept and gives answers that are too low. Another is using the deduped length instead of the original n for the range width. Check example 3: [1,10,100,1000], n=4, every window keeps one value, so the answer is 3. Complexity is O(n log n) from sorting. If your mind goes blank during the live OA, StealthCoder is the hedge that reads the problem and hands you this approach.
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 Minimum Operations to Make an Array Continuous 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 Amazon's OA.
Amazon 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.
Minimum Operations to Make an Array Continuous FAQ
What's the trick for Minimum Operations to Make an Array Continuous?+
Dedupe and sort, then slide a window over the unique values where max minus min is at most n - 1. The largest window is the most values you can keep. Answer is n minus that size. Duplicates always need replacing, so they never count as kept.
How hard is this Amazon OA question really?+
Medium-hard on paper, easy once you see the reduction. The code is about ten lines. The difficulty is realizing the final array is just a range of n integers, and that you're maximizing overlap with existing values.
Why do I have to remove duplicates first?+
The continuous array needs all unique elements. If you keep two equal values, only one can stay in the final array. Using a set before sorting guarantees each kept value is distinct, so your window count is honest.
What's the time complexity I should aim for?+
O(n log n) is the target, driven by sorting the unique values. The two-pointer pass afterward is O(n). A brute force that tries every start value and counts is O(n^2) and risks timing out on large inputs.
How do I prepare for this in 48 hours?+
Write the sort plus two-pointer solution from scratch twice. Test with the three given examples, plus an input with duplicates like [1,1,1] and a single-element array. Then do a few other sliding window problems on sorted data so the pattern feels automatic.