Smallest Missing Integer Greater Than Sequential Prefix Sum - Problem

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.

Input & Output

Example 1 — Sequential Start
$ Input: nums = [1,2,3,5,6]
Output: 7
💡 Note: Sequential prefix is [1,2,3] with sum 6. Numbers 6 exists in array, but 7 is missing, so return 7.
Example 2 — Single Element Prefix
$ Input: nums = [1,3,6,4,1,6]
Output: 2
💡 Note: Sequential prefix is [1] with sum 1. Check: 1 exists, 2 is missing, so return 2.
Example 3 — Longer Sequence
$ Input: nums = [3,4,5,1,12,14,13]
Output: 15
💡 Note: Sequential prefix is [3,4,5] with sum 12. Numbers 12, 13, 14 all exist, but 15 is missing, so return 15.

Constraints

  • 1 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 50

Visualization

Tap to expand
Smallest Missing Integer Greater Than Sequential Prefix Sum INPUT Array nums: 1 i=0 2 i=1 3 i=2 5 i=3 6 i=4 Sequential Prefix: [1, 2, 3] 1+1=2 (OK), 2+1=3 (OK) 3+1=4 != 5 (STOP) Hash Set: {1, 2, 3, 5, 6} Prefix Sum = 1+2+3 = 6 ALGORITHM STEPS 1 Find Sequential Prefix Iterate while nums[j]=nums[j-1]+1 2 Calculate Sum Sum = 1+2+3 = 6 3 Build Hash Set Store all nums in set 4 Find Missing Search from sum onwards Search Process: x=6: in set SKIP x=7: not in set FOUND! Return 7 FINAL RESULT Longest Sequential Prefix: [1, 2, 3] Prefix Sum: 6 Numbers in array: {1, 2, 3, 5, 6} Missing from sum (6): OUTPUT 7 Smallest missing integer greater than or equal to 6 Key Insight: Using a Hash Set allows O(1) lookup for each candidate number. We find the longest sequential prefix (elements differing by 1), calculate its sum, then search from that sum upward until we find a number not in the set. Time: O(n), Space: O(n). TutorialsPoint - Smallest Missing Integer Greater Than Sequential Prefix Sum | Hash Set Optimization
Asked in
Google 15 Microsoft 12 Amazon 8
8.5K Views
Medium Frequency
~15 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen