Smallest Missing Integer Greater Than Sequential Prefix Sum (LeetCode 2996): Solution and Explanation
Add up the longest prefix where each element is one more than the last, then return the smallest value at or above that sum that is not in the array.
Intuition
The problem has two separate parts. First find a number S, then search upward from S.
The sequential prefix is a staircase that starts at nums[0] and climbs by exactly 1 each step. The first element that does not climb by 1 ends it for good, even if later elements would climb again.
Once S is known, the answer is the first value S, S + 1, S + 2, ... that does not appear in nums. A set makes each of those checks O(1).
The upward walk is short. Only n values can block it, so it stops after at most n + 1 checks.
Approach: Prefix scan, then probe a set
- Set
total = nums[0]andi = 1. - While
i < nandnums[i] == nums[i - 1] + 1, addnums[i]tototaland moveiforward. - Build a set of all values in
nums. - While
totalis in the set, add 1 to it. Returntotal.
Step-by-step example
nums = [1,2,3,2,5]
- Start with total = 1.
- 2 is 1 + 1, so total = 3. 3 is 2 + 1, so total = 6.
- 2 is not 3 + 1, so the prefix [1,2,3] ends here with sum 6.
- The set is {1, 2, 3, 5}. 6 is not in it, so return 6.
Code: Python, Java, C++
1class Solution:
2 def missingInteger(self, nums: List[int]) -> int:
3 total = nums[0]
4 i = 1
5 while i < len(nums) and nums[i] == nums[i - 1] + 1:
6 total += nums[i]
7 i += 1
8 seen = set(nums)
9 while total in seen:
10 total += 1
11 return total1class Solution {
2 public int missingInteger(int[] nums) {
3 int total = nums[0];
4 int i = 1;
5 while (i < nums.length && nums[i] == nums[i - 1] + 1) {
6 total += nums[i];
7 i++;
8 }
9 Set<Integer> seen = new HashSet<>();
10 for (int x : nums) {
11 seen.add(x);
12 }
13 while (seen.contains(total)) {
14 total++;
15 }
16 return total;
17 }
18}1class Solution {
2public:
3 int missingInteger(vector<int>& nums) {
4 int total = nums[0];
5 int i = 1;
6 while (i < (int)nums.size() && nums[i] == nums[i - 1] + 1) {
7 total += nums[i];
8 i++;
9 }
10 unordered_set<int> seen(nums.begin(), nums.end());
11 while (seen.count(total)) {
12 total++;
13 }
14 return total;
15 }
16};Time and space complexity
O(n)
The prefix scan and building the set are each one pass, and the upward walk takes at most n + 1 steps.
O(n)
The set holds up to n values.
Other approaches
Boolean array (time O(n), space O(1))
Values are at most 50, so a fixed array of flags can replace the set. Any total above 50 is missing right away.
Sort then scan (time O(n log n), space O(n))
Sort the values and walk them while they match the candidate. It works but the sort is unnecessary.
Common mistakes
- Searching for the longest sequential run anywhere in the array. Only the prefix that starts at index 0 counts.
- Continuing the scan after the first break. [1,2,5,6] has prefix [1,2], not [1,2] plus [5,6].
- Returning the sum without checking whether it already appears in nums, as in [3,4,5,1,12,14,13], where 12 is present.
- Checking membership with a linear search inside the loop. It still passes the small limits, but a set keeps the solution linear.
Frequently asked questions
Does a one-element array have a sequential prefix?
Yes. nums[0] by itself always counts, so S is at least nums[0].
Why does the upward walk always end?
The array has only n values, so among any n + 1 consecutive integers at least one is missing.
Can the answer be smaller than the prefix sum?
No. The answer must be at least the sum, even if smaller values are missing from the array.