githubEdit

19. Missing Element in Range

βœ… GFG solution to Missing Element in Range: find all numbers within a given range not present in array using efficient marking technique. πŸš€

The problem can be found at the following link: πŸ”— Question Linkarrow-up-right

🧩 Problem Description

Given an array arr[] of integers and a range [low, high], find all the numbers within the range that are not present in the array. Return the missing numbers in sorted order.

πŸ“˜ Examples

Example 1

Input: arr[] = [10, 12, 11, 15], low = 10, high = 15
Output: [13, 14]
Explanation: Numbers 13 and 14 lie in the range [10, 15] but are not present in the array.

Example 2

Input: arr[] = [1, 4, 11, 51, 15], low = 50, high = 55
Output: [50, 52, 53, 54, 55]
Explanation: Numbers 50, 52, 53, 54 and 55 lie in the range [50, 55] but are not present in the array.

Example 3

Input: arr[] = [5, 6, 7, 8, 9], low = 1, high = 10
Output: [1, 2, 3, 4, 10]
Explanation: Numbers 1, 2, 3, 4, and 10 are missing from the range [1, 10].

πŸ”’ Constraints

  • $1 \le \text{arr.size()}, \text{low}, \text{high} \le 10^5$

  • $1 \le \text{arr}[i] \le 10^5$

βœ… My Approach

The optimal solution uses Boolean Array Marking to efficiently track present elements:

Boolean Marking with Range Filtering

  1. Create Marking Array:

    • Build a boolean array of size (high - low + 1) initialized to false.

    • This represents all positions in the range [low, high].

  2. Mark Present Elements:

    • Iterate through the input array.

    • For each element x that falls within [low, high], mark present[x - low] = true.

    • Ignore elements outside the range (optimization over set approach).

  3. Collect Missing Numbers:

    • Iterate through the boolean array.

    • For each false position at index i, add low + i to the result.

    • The result is automatically sorted due to sequential iteration.

  4. Return Result:

    • Return the collected missing numbers.

Key Advantage: This approach only processes elements within the range and provides O(1) lookup and marking time, making it more efficient than set-based approaches for small to medium ranges.

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n + r), where n is the size of the array and r is the range size (high - low + 1). We iterate through the array once to mark present elements O(n), then iterate through the range once to collect missing elements O(r).

  • Expected Auxiliary Space Complexity: O(r), where r is the range size (high - low + 1). We use a boolean array of size equal to the range, which is more space-efficient than a set when the range is smaller than the array size.

πŸ§‘β€πŸ’» Code (C++)

chevron-right⚑ View Alternative Approaches with Code and Analysishashtag

πŸ“Š 2️⃣ Set-Based Approach

πŸ’‘ Algorithm Steps:

  1. Insert all array elements into an unordered set for O(1) average lookup.

  2. Iterate through the range [low, high] sequentially.

  3. For each number, check if it exists in the set.

  4. Collect all numbers not found in the set into result vector.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n + r) - Set creation O(n), range iteration O(r)

  • Auxiliary Space: πŸ’Ύ O(n) - Set stores all n elements

βœ… Why This Approach?

  • Simple and straightforward implementation

  • Good when array size is much smaller than range

  • Intuitive logic flow for beginners

πŸ“Š 3️⃣ Sorting with Two Pointers

πŸ’‘ Algorithm Steps:

  1. Sort the input array in ascending order.

  2. Use two pointers: one for the current range number and one for array index.

  3. For each number in [low, high], check if it matches the current array element.

  4. If match found, move array pointer; otherwise, add to missing list.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n log n + r) - Sorting plus range traversal

  • Auxiliary Space: πŸ’Ύ O(1) - Excluding output array

βœ… Why This Approach?

  • Space efficient without additional data structures

  • Natural for problems requiring sorted input

  • Good when array contains many duplicates

πŸ“Š 4️⃣ Binary Search on Sorted Array (Optional)

πŸ’‘ Algorithm Steps:

  1. Sort the input array for binary search capability.

  2. For each number in the range [low, high], perform binary search.

  3. If binary search fails to find the number, add it to result.

  4. Return collected missing numbers.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n log n + r log n) - Sorting plus r binary searches

  • Auxiliary Space: πŸ’Ύ O(1) - Constant extra space

βœ… Why This Approach?

  • Leverages standard library functions

  • Good when range is small but array is large

  • Clean and concise implementation

πŸ“Š 4️⃣ Optimized Set with Range Pre-filtering

πŸ’‘ Algorithm Steps:

  1. Create a set but only insert elements that fall within [low, high].

  2. This reduces memory usage when array has many out-of-range elements.

  3. Iterate through range and check against filtered set.

  4. Collect missing numbers into result.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(n + r) - Linear passes through array and range

  • Auxiliary Space: πŸ’Ύ O(min(n, r)) - Set size limited by range

βœ… Why This Approach?

  • Memory efficient when many elements are out of range

  • Combines benefits of set and filtering

  • Better cache locality than full set

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

🎯 Boolean Marking

🟒 O(n + r)

🟒 O(r)

πŸš€ Fast, predictable memory

πŸ’Ύ High space for large ranges

πŸ” Set-Based

🟒 O(n + r)

🟑 O(n)

πŸ“– Simple implementation

πŸ’Ύ Stores all elements

πŸ“Š Sorting + Two Pointers

🟑 O(n log n + r)

🟒 O(1)

πŸ’Ύ Space efficient

🐌 Sorting overhead

πŸ”Ž Binary Search (Optional)

πŸ”΄ O(n log n + r log n)

🟒 O(1)

🎯 Uses standard library

🐌 Repeated binary searches

πŸ”§ Filtered Set

🟒 O(n + r)

🟒 O(min(n,r))

⚑ Memory optimized

πŸ”§ Extra condition checks

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ”₯ Performance Rating

πŸ… Small to medium range size

πŸ₯‡ Boolean Marking

β˜…β˜…β˜…β˜…β˜…

πŸ“– Simple implementation needed

πŸ₯ˆ Set-Based

β˜…β˜…β˜…β˜…β˜†

πŸ’Ύ Memory constrained

πŸ₯‰ Sorting + Two Pointers

β˜…β˜…β˜…β˜…β˜†

🎯 Large range, small array

πŸ… Filtered Set

β˜…β˜…β˜…β˜…β˜…

β˜• Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?arrow-up-right. Let's make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated