Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,3,2,5]
[3,4,5,1,12,14,13]
longest-common-prefix)first-missing-positive)next-greater-element-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2996: Smallest Missing Integer Greater Than Sequential Prefix Sum
class Solution {
public int missingInteger(int[] nums) {
int s = nums[0];
for (int j = 1; j < nums.length && nums[j] == nums[j - 1] + 1; ++j) {
s += nums[j];
}
boolean[] vis = new boolean[51];
for (int x : nums) {
vis[x] = true;
}
for (int x = s;; ++x) {
if (x >= vis.length || !vis[x]) {
return x;
}
}
}
}
// Accepted solution for LeetCode #2996: Smallest Missing Integer Greater Than Sequential Prefix Sum
func missingInteger(nums []int) int {
s := nums[0]
for j := 1; j < len(nums) && nums[j] == nums[j-1]+1; j++ {
s += nums[j]
}
vis := [51]bool{}
for _, x := range nums {
vis[x] = true
}
for x := s; ; x++ {
if x >= len(vis) || !vis[x] {
return x
}
}
}
# Accepted solution for LeetCode #2996: Smallest Missing Integer Greater Than Sequential Prefix Sum
class Solution:
def missingInteger(self, nums: List[int]) -> int:
s, j = nums[0], 1
while j < len(nums) and nums[j] == nums[j - 1] + 1:
s += nums[j]
j += 1
vis = set(nums)
for x in count(s):
if x not in vis:
return x
// Accepted solution for LeetCode #2996: Smallest Missing Integer Greater Than Sequential Prefix Sum
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2996: Smallest Missing Integer Greater Than Sequential Prefix Sum
// class Solution {
// public int missingInteger(int[] nums) {
// int s = nums[0];
// for (int j = 1; j < nums.length && nums[j] == nums[j - 1] + 1; ++j) {
// s += nums[j];
// }
// boolean[] vis = new boolean[51];
// for (int x : nums) {
// vis[x] = true;
// }
// for (int x = s;; ++x) {
// if (x >= vis.length || !vis[x]) {
// return x;
// }
// }
// }
// }
// Accepted solution for LeetCode #2996: Smallest Missing Integer Greater Than Sequential Prefix Sum
function missingInteger(nums: number[]): number {
let s = nums[0];
for (let j = 1; j < nums.length && nums[j] === nums[j - 1] + 1; ++j) {
s += nums[j];
}
const vis: Set<number> = new Set(nums);
for (let x = s; ; ++x) {
if (!vis.has(x)) {
return x;
}
}
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.