Algorithm to determine if array contains n...nm?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If the question is whether an array contains every integer from n through n + m, the problem is really a membership check over a fixed numeric interval. The simplest correct solution is usually a set-based test, but there are a couple of useful variants depending on whether duplicates matter and whether you want linear time with or without extra memory.
Clarify The Requirement First
There are two common interpretations:
- the array contains all integers in the interval somewhere, in any order
- the array contains them as a consecutive block with no extras
Most versions of the problem mean the first one. For example, if n = 5 and m = 4, then the required set is:
5, 6, 7, 8, 9
An array such as [9, 5, 7, 6, 8] should return true.
Straightforward Set-Based Solution
The clearest solution is to turn the array into a set and verify each required value.
This runs in expected O(k + m) time, where k is the array length, because set membership is constant-time on average.
Early Rejection Checks
Before building any set, you can cheaply reject impossible cases.
If the interval from n to n + m contains m + 1 distinct values, then an array with fewer than m + 1 elements cannot possibly contain all of them unless duplicates are irrelevant and the array is longer elsewhere.
A quick guard looks like this:
This does the same logical work, but the early length check can rule out bad inputs immediately.
When Sorting Makes Sense
A sorting-based approach is not usually optimal for pure membership, but it can be useful if the data is already sorted or if you need the values ordered for later processing.
This is less attractive than the set solution for the basic problem because sorting costs O(k log k).
Constant-Space Idea Using Min And Max
If the problem also guarantees:
- exactly
m + 1relevant values - no duplicates among the target interval entries
- no extras outside the interval
then you can sometimes use a min/max plus sum or XOR style check. But that version is more fragile because duplicates can fool naive arithmetic tests.
For general-purpose code, the set-based solution is safer and easier to reason about.
Example Walkthrough
Suppose the array is:
and you want to know whether it contains 12 through 15.
The set becomes:
Now checking 12, 13, 14, and 15 is trivial. The extra value 99 does not matter because the problem only asks whether the required interval is present.
Common Pitfalls
A common mistake is checking only that min(values) == n and max(values) == n + m. That is not enough. The array [5, 5, 5, 9] has the right min and max but does not contain 6, 7, or 8.
Another issue is forgetting whether duplicates are allowed. Duplicates do not hurt a set-based solution, but they do break some arithmetic shortcuts.
Developers also sometimes overuse sorting when membership alone is enough. If order does not matter, hashing is usually the better fit.
Finally, handle negative m explicitly. An interval from n to n + m only makes sense when the range definition is valid for your problem.
Summary
- The safest general solution is to put the array values in a set and test whether
nthroughn + mare all present. - This gives expected linear-time behavior with simple code.
- Sorting works but is usually slower for the pure membership version of the problem.
- Min/max checks alone are not sufficient because they do not prove every intermediate value exists.
- Arithmetic shortcuts are only safe under stronger assumptions than the general problem usually provides.

