VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-an-array-contains-all-elements-of-a-given-range/

⇱ Range elements are present in an array or not - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Range elements are present in an array or not

Last Updated : 16 May, 2026

An array containing positive elements is given. 'start' and 'end' are two numbers defining a range. Write a function to check if the array contains all elements in the given range.

Note: If the array contains all elements in the given range return true otherwise return false.

Examples :

Input : arr[] = {1, 4, 5, 2, 7, 8, 3} , start = 2, end = 5
Output : true
Explanation: All elements of given range are present

Input : arr[] = {1, 4, 5, 2, 7, 8, 3} , start = 2, end = 6
Output : false
Explanation: 6 is missing

[Naive Approach] Check All Using Linear Search - O(n × (end - start)) Time and O(1) Space

The idea is to verify whether all integers in the given range [start, end] are present in the array. For every number from start to end, we scan the entire array to check if that number exists. If any number in the range is missing, we immediately return false. If all numbers are found, we return true.

Consider the following dry run : arr[] = {1, 4, 5, 2, 7, 8, 3} , start = 2, end = 6

  • For i = 2 --> Search 2 in array --> 1 != 2, 4 != 2, 5 != 2, 2 == 2 found at index 3 --> continue
  • For i = 3 --> Search 3 in array --> 1 != 3, 4 != 3, 5 != 3, 2 != 3, 7 != 3, 8 != 3, 3 == 3 found at index 6 --> continue
  • For i = 4 --> Search 4 in array --> 1 != 4, 4 == 4 found at index 1 --> continue
  • For i = 5 --> Search 5 in array --> 1 != 5, 4 != 5, 5 == 5 found at index 2 --> continue
  • For i = 6 --> Search 6 in array --> 1 != 6, 4 != 6, 5 != 6, 2 != 6, 7 != 6, 8 != 6, 3 != 6 --> 6 not found in array --> return false

Final answer : False


Output
True

[Optimal Approach] Using Hashing - O(n + (end - start)) Time and O(n) Space

The intuition is to store all array elements in a hash set for fast lookup. Traverse every number from start to end and check whether it exists in the set. If any number is missing, return false; otherwise, return true.

Consider the following dry run : arr[] = {1, 4, 5, 2, 7, 8, 3} , start = 2, end = 6

Store all array elements in a hash set : {1, 2, 3, 4, 5, 7, 8} (order need not be same)

For i = 2 --> 2 found in hash set --> continue
For i = 3 --> 3 found in hash set --> continue
For i = 4 --> 4 found in hash set --> continue
For i = 5 --> 5 found in hash set --> continue
For i = 6 --> 6 not found in hash set --> return false

Final answer : False


Output
True

[Optimal Approach] In-Place Index Mapping - O(n) Time and O(1) Space

Since the array contains only positive numbers, we use negative marking to track which values are present. Every value in the range start to end is mapped to an index and that index is marked negative to indicate presence. After marking, if any required index remains positive, it means that number is missing from the array.

Algorithm:

  • Traverse the array linearly. If an element lies in the range [start, end], then mark the element at index "arr[i] - start" as negative.
  • A negative value at an index indicates that the corresponding number is present in the array.
  • Finally, check all mapped indices from 0 to (end - start). If any index still contains a positive value, then some number in the range is missing; otherwise, all numbers are present.

Consider the following dry run :
arr[] = {2, 4, 3, 7} , start = 2, end = 5, required range size = end - start + 1 = 4

Marking presence using index mapping :

  • For i = 0 --> val = |2| = 2 --> idx = 2 - 2 = 0 --> arr[0] = 2 > 0 --> mark negative so array becomes {-2, 4, 3, 7}
  • For i = 1 --> val = |4| = 4 --> idx = 4 - 2 = 2 --> arr[2] = 3 > 0 --> mark negative so array becomes {-2, 4, -3, 7}
  • For i = 2 --> val = |-3| = 3 --> idx = 3 - 2 = 1 --> arr[1] = 4 > 0 --> mark negative so
    array becomes {-2, -4, -3, 7}
  • For i = 3 --> val = |7| = 7 --> 7 not in range [2, 5] --> continue

Check all required indices (verification phase) :

  • For i = 0 --> arr[0] = -2 < 0 --> number 2 exists
  • For i = 1 --> arr[1] = -4 < 0 --> number 3 exists
  • For i = 2 --> arr[2] = -3 < 0 --> number 4 exists
  • For i = 3 --> arr[3] = 7 > 0 --> number 5 missing --> return false

Output
True
Comment
Article Tags: