Watch 10 video solutions for Smallest Missing Integer Greater Than Sequential Prefix Sum, a easy level problem involving Array, Hash Table, Sorting. This walkthrough by Aryan Mittal has 1,991 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 0-indexed array of integers nums.
A prefix nums[0..i] is sequential if, for all 1 <= j <= i, nums[j] = nums[j - 1] + 1. In particular, the prefix consisting only of nums[0] is sequential.
Return the smallest integer x missing from nums such that x is greater than or equal to the sum of the longest sequential prefix.
Example 1:
Input: nums = [1,2,3,2,5] Output: 6 Explanation: The longest sequential prefix of nums is [1,2,3] with a sum of 6. 6 is not in the array, therefore 6 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
Example 2:
Input: nums = [3,4,5,1,12,14,13] Output: 15 Explanation: The longest sequential prefix of nums is [3,4,5] with a sum of 12. 12, 13, and 14 belong to the array while 15 does not. Therefore 15 is the smallest missing integer greater than or equal to the sum of the longest sequential prefix.
Constraints:
1 <= nums.length <= 501 <= nums[i] <= 50Problem Overview: You are given an integer array. First identify the longest prefix where numbers increase sequentially by exactly +1. Compute the sum of that prefix. The task is to return the smallest integer greater than or equal to that sum that does not appear in the array.
The challenge has two parts: detect the longest sequential prefix and then search for the smallest missing integer starting from the prefix sum. Efficient solutions rely on constant-time lookups using a hash table or ordering the numbers with sorting. Since the input is an array, a single pass combined with fast membership checks usually gives the optimal result.
Approach 1: Identify Longest Sequential Prefix and Check Missing Integer (O(n) time, O(n) space)
Scan the array from the start and extend the prefix while nums[i] == nums[i-1] + 1. Accumulate the prefix sum during this pass. Once the sequential prefix ends, insert every value from the array into a hash set for constant-time lookups. Start checking integers from the computed prefix sum. If the value exists in the set, increment and check again. The first value not present is the answer.
The key insight: the prefix sum defines the lower bound for the result, while the hash set quickly tells you whether a candidate value already appears in the array. This avoids repeatedly scanning the array and keeps the search for the missing number efficient.
Approach 2: Tracking Sequence with a Sorted Array (O(n log n) time, O(1) extra space)
Another strategy sorts the array first. After computing the sequential prefix and its sum, iterate through the sorted numbers to see if the candidate value exists. If the prefix sum is present, increment it until you encounter a gap. Because the array is sorted, duplicates and smaller values are easy to skip while advancing the candidate.
This approach trades extra runtime for reduced auxiliary memory. Sorting organizes the values so you can detect the first missing integer through a linear scan. It works well when modifying the input array is allowed and additional memory for a hash structure is undesirable.
Recommended for interviews: The hash set approach is typically expected. It separates the problem into two clear steps: compute the sequential prefix sum, then perform fast membership checks. Time complexity stays O(n) with O(n) space, which is optimal for arbitrary arrays. Showing the sorting alternative demonstrates awareness of space–time tradeoffs, but the hash-based solution best reflects strong problem-solving instincts.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Identify Sequential Prefix + Hash Set Lookup | O(n) | O(n) | General case. Best performance with constant-time membership checks. |
| Sort Array and Scan for Missing Integer | O(n log n) | O(1) extra | When extra memory is limited and modifying the array is acceptable. |