algorithms
array processing
number sequence
programming
data structures

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.

python
1def contains_range(values, n, m):
2    if m < 0:
3        return False
4
5    needed = set(range(n, n + m + 1))
6    present = set(values)
7    return needed.issubset(present)
8
9
10print(contains_range([9, 5, 7, 6, 8], 5, 4))
11print(contains_range([5, 6, 8, 9], 5, 4))

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:

python
1def contains_range_fast(values, n, m):
2    if m < 0:
3        return False
4    if len(values) < m + 1:
5        return False
6
7    present = set(values)
8    for x in range(n, n + m + 1):
9        if x not in present:
10            return False
11    return True

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.

python
1def contains_range_sorted(values, n, m):
2    sorted_values = sorted(set(values))
3    target = list(range(n, n + m + 1))
4
5    for i in range(len(sorted_values) - len(target) + 1):
6        if sorted_values[i:i + len(target)] == target:
7            return True
8    return False

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 + 1 relevant 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:

text
[12, 15, 14, 13, 99]

and you want to know whether it contains 12 through 15.

The set becomes:

text
{12, 13, 14, 15, 99}

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 n through n + m are 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.

Course illustration
Course illustration

All Rights Reserved.