Smallest Missing Integer Greater Than Sequential Prefix Sum - Problem

You're given a 0-indexed array of integers nums. Your task is to find the smallest missing integer that meets a specific condition.

What makes a prefix "sequential"?
A prefix nums[0..i] is sequential if each element (starting from index 1) is exactly one more than the previous element. In other words, nums[j] = nums[j-1] + 1 for all 1 ≤ j ≤ i.

Your Mission:
Find the longest sequential prefix, calculate its sum, then return the smallest integer missing from the array that is greater than or equal to this sum.

Example walkthrough:
For [4, 5, 6, 7, 6, 1, 1]:
• Longest sequential prefix: [4, 5, 6, 7] (length 4)
• Sum: 4 + 5 + 6 + 7 = 22
• Missing integers ≥ 22: We need to check 22, 23, 24... until we find one not in the array
• Answer: The first missing integer ≥ 22

Input & Output

example_1.py — Basic Sequential Prefix
$ Input: [4, 5, 6, 7, 6, 1, 1]
Output: 22
💡 Note: The longest sequential prefix is [4, 5, 6, 7] with sum 22. Since 22 is not in the array, it's the smallest missing integer ≥ 22.
example_2.py — Single Element Prefix
$ Input: [1, 3, 6, 4, 1, 2]
Output: 2
💡 Note: The longest sequential prefix is just [1] with sum 1. The smallest missing integer ≥ 1 is 2 (since 1 exists but 2 doesn't).
example_3.py — All Elements Sequential
$ Input: [3, 4, 5, 6]
Output: 18
💡 Note: The entire array [3, 4, 5, 6] is sequential with sum 18. Since 18 is not in the array, it's the answer.

Constraints

  • 1 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 50
  • The array is 0-indexed
  • A prefix consisting only of nums[0] is always considered sequential

Visualization

Tap to expand
🕵️‍♂️ Sequential Prefix Detective📁 Case Files StackFile 4File 5File 6File 7File 6❌ Sequence broken!✓ Consecutive files: 4→5→6→7 (Sum = 22)📋 Evidence Catalog (Hash Set)Available Files: {1, 4, 5, 6, 7}🔍 Quick lookup catalog for O(1) searches🎯 Missing File Detection (Starting from Sum = 22)Check 2222 ∈ Catalog? → NO! ❌🏆 FOUND: 22First missing file!🎯 Detective's Key Insight"Once I know the evidence sum (22), I only need to check consecutivefile numbers starting from 22 until I find the first missing one!"⚡ Time: O(n) | Space: O(n) | Optimal Solution!
Understanding the Visualization
1
Find Consecutive Evidence
Starting from the first file, count how many files are numbered consecutively: [4,5,6,7] then breaks at 6
2
Calculate Evidence Sum
Sum up all the consecutive file numbers: 4+5+6+7 = 22
3
Create Evidence Catalog
List all available file numbers in a quick-lookup catalog: {4,5,6,7,6,1,1}
4
Find Missing File
Starting from sum=22, check catalog for first missing file number: 22 is missing!
Key Takeaway
🎯 Key Insight: The problem becomes much simpler when we realize that after finding the sequential prefix sum, we only need to check consecutive integers starting from that sum. Using a hash set for O(1) lookups makes this extremely efficient, giving us the optimal O(n) solution!
Asked in
Google 23 Amazon 18 Microsoft 15 Meta 12
26.8K Views
Medium Frequency
~15 min Avg. Time
892 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