Smallest Missing Integer Greater Than Sequential Prefix Sum (LeetCode 2996): Solution and Explanation

Easy
Array
Hash Table
Sorting

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

  1. Set total = nums[0] and i = 1.
  2. While i < n and nums[i] == nums[i - 1] + 1, add nums[i] to total and move i forward.
  3. Build a set of all values in nums.
  4. While total is in the set, add 1 to it. Return total.

Step-by-step example

nums = [1,2,3,2,5]
  1. Start with total = 1.
  2. 2 is 1 + 1, so total = 3. 3 is 2 + 1, so total = 6.
  3. 2 is not 3 + 1, so the prefix [1,2,3] ends here with sum 6.
  4. 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 total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Prefix scan, then probe a set O(n)
Boolean array O(n)
Sort then scan O(n log n)
Time

O(n)

The prefix scan and building the set are each one pass, and the upward walk takes at most n + 1 steps.

Space

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.