Check if an array contains all elements of a given range in Python

Suppose we have an array called nums and two numbers x and y defining a range [x, y]. We need to check whether the array contains all elements in the given range or not.

So, if the input is like nums = [5,8,9,6,3,2,4], x = 2, y = 6, then the output will be True as the array contains all elements [2,3,4,5,6] from the range.

Using Set Intersection Method

The simplest approach is to convert the array to a set and check if all range elements are present ?

def check_range_simple(nums, x, y):
    nums_set = set(nums)
    range_set = set(range(x, y + 1))
    return range_set.issubset(nums_set)

nums = [5, 8, 9, 6, 3, 2, 4]
x = 2
y = 6
print(check_range_simple(nums, x, y))
True

Using Array Marking Method

This method marks elements in the array by making them negative to track which range elements are found ?

def check_range_marking(nums, x, y):
    # Create a copy to avoid modifying original array
    nums_copy = nums.copy()
    temp_range = y - x
    
    # Mark elements that are in range
    for i in range(len(nums_copy)):
        if abs(nums_copy[i]) >= x and abs(nums_copy[i]) <= y:
            z = abs(nums_copy[i]) - x
            if z < len(nums_copy) and nums_copy[z] > 0:
                nums_copy[z] = -nums_copy[z]
    
    # Count marked elements
    cnt = 0
    for i in range(temp_range + 1):
        if i >= len(nums_copy):
            break
        if nums_copy[i] > 0:
            return False
        else:
            cnt += 1
    
    return cnt == temp_range + 1

nums = [5, 8, 9, 6, 3, 2, 4]
x = 2
y = 6
print(check_range_marking(nums, x, y))
True

Using List Comprehension

A more Pythonic approach using list comprehension to filter elements ?

def check_range_comprehension(nums, x, y):
    range_elements = [i for i in range(x, y + 1)]
    return all(element in nums for element in range_elements)

nums = [5, 8, 9, 6, 3, 2, 4]
x = 2
y = 6
print(check_range_comprehension(nums, x, y))
True

Testing with Different Examples

Let's test our methods with arrays that don't contain all range elements ?

# Test case where not all elements are present
nums_incomplete = [2, 4, 6, 8]
x = 2
y = 6

print("Set method:", check_range_simple(nums_incomplete, x, y))
print("Marking method:", check_range_marking(nums_incomplete, x, y))
print("Comprehension method:", check_range_comprehension(nums_incomplete, x, y))
Set method: False
Marking method: False
Comprehension method: False

Comparison

Method Time Complexity Space Complexity Best For
Set Intersection O(n + m) O(n + m) Simple and readable
Array Marking O(n) O(n) Memory efficient
List Comprehension O(n × m) O(m) Pythonic style

Where n is the array length and m is the range size (y - x + 1).

Conclusion

Use the set intersection method for simplicity and readability. The array marking method is memory-efficient but more complex. Choose based on your specific requirements for performance and code clarity.

Updated on: 2026-03-25T14:16:14+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements