🧠
Brute Force (Sorting + Linear Scan)
💡 This approach exists to build intuition by sorting the array and scanning for missing positive ranges. It is straightforward but not optimal for large inputs.
Intuition
Sort the array, then scan from 1 upwards to find the smallest missing positive range by checking gaps between consecutive positive numbers.
Algorithm
- Sort the array.
- Initialize a variable 'expected' to 1, representing the smallest positive integer expected.
- Iterate through the sorted array, skipping non-positive numbers and duplicates.
- For each positive number, if it is greater than 'expected', then the missing range is from 'expected' to the number before the current number.
- If no gaps found, the missing range is from 'expected' to 'expected'.
💡 Sorting helps us see numbers in order, making it easier to spot missing ranges by comparing expected and actual numbers.
def find_smallest_missing_range(nums):
nums.sort()
expected = 1
i = 0
n = len(nums)
while i < n and nums[i] < expected:
i += 1
while i < n:
if nums[i] == expected:
expected += 1
elif nums[i] > expected:
return [expected, nums[i] - 1]
i += 1
return [expected, expected]
# Driver code
if __name__ == '__main__':
print(find_smallest_missing_range([3, 4, -1, 1])) # Output: [2, 2]
Line Notes
nums.sort()Sort the array to bring numbers in ascending order for easy gap detection
expected = 1Start looking for missing positive integers from 1
while i < n and nums[i] < expected:Skip all numbers less than expected (non-positive or duplicates)
if nums[i] == expected:If current number matches expected, increment expected to look for next
elif nums[i] > expected:If current number is greater than expected, gap found; return missing range
return [expected, expected]If no gaps found, missing range is the next expected number
import java.util.*;
public class Main {
public static int[] findSmallestMissingRange(int[] nums) {
Arrays.sort(nums);
int expected = 1;
int i = 0, n = nums.length;
while (i < n && nums[i] < expected) i++;
while (i < n) {
if (nums[i] == expected) expected++;
else if (nums[i] > expected) return new int[]{expected, nums[i] - 1};
i++;
}
return new int[]{expected, expected};
}
public static void main(String[] args) {
int[] nums = {3, 4, -1, 1};
int[] res = findSmallestMissingRange(nums);
System.out.println("[" + res[0] + ", " + res[1] + "]");
}
}
Line Notes
Arrays.sort(nums);Sort the array to detect missing ranges easily
int expected = 1;Initialize expected smallest positive integer
while (i < n && nums[i] < expected) i++;Skip non-positive and smaller numbers
if (nums[i] == expected) expected++;Increment expected if current number matches
else if (nums[i] > expected) return new int[]{expected, nums[i] - 1};Return missing range if gap found
return new int[]{expected, expected};Return next expected if no gaps
#include <bits/stdc++.h>
using namespace std;
pair<int,int> findSmallestMissingRange(vector<int>& nums) {
sort(nums.begin(), nums.end());
int expected = 1, i = 0, n = nums.size();
while (i < n && nums[i] < expected) i++;
while (i < n) {
if (nums[i] == expected) expected++;
else if (nums[i] > expected) return {expected, nums[i] - 1};
i++;
}
return {expected, expected};
}
int main() {
vector<int> nums = {3, 4, -1, 1};
auto res = findSmallestMissingRange(nums);
cout << "[" << res.first << ", " << res.second << "]\n";
return 0;
}
Line Notes
sort(nums.begin(), nums.end());Sort array to find missing ranges by scanning
int expected = 1;Start checking from smallest positive integer
while (i < n && nums[i] < expected) i++;Skip negative and smaller numbers
if (nums[i] == expected) expected++;Increment expected if current number matches
else if (nums[i] > expected) return {expected, nums[i] - 1};Return missing range if gap found
return {expected, expected};Return next expected if no gaps found
function findSmallestMissingRange(nums) {
nums.sort((a,b) => a - b);
let expected = 1, i = 0, n = nums.length;
while (i < n && nums[i] < expected) i++;
while (i < n) {
if (nums[i] === expected) expected++;
else if (nums[i] > expected) return [expected, nums[i] - 1];
i++;
}
return [expected, expected];
}
// Test
console.log(findSmallestMissingRange([3, 4, -1, 1])); // [2, 2]
Line Notes
nums.sort((a,b) => a - b);Sort array ascending to detect missing ranges
let expected = 1;Start from smallest positive integer
while (i < n && nums[i] < expected) i++;Skip negatives and smaller numbers
if (nums[i] === expected) expected++;Increment expected if current number matches
else if (nums[i] > expected) return [expected, nums[i] - 1];Return missing range if gap found
return [expected, expected];Return next expected if no gaps
TimeO(n log n)
SpaceO(1) or O(n) depending on sorting implementation
Sorting takes O(n log n), scanning is O(n). Sorting dominates time complexity.
💡 For n=100,000, sorting takes about 1.5 million operations, which is slow but acceptable for medium inputs.
Interview Verdict: Accepted but not optimal
This approach works but is not efficient for very large inputs; it helps understand the problem and leads to better solutions.