You are given an array nums
of length n
consisting of positive integers. The goal is to find the smallest missing integer greater than the sequential prefix sum of the array.
The “sequential prefix sum” of the array is a new array where each element at index i
is the sum of all previous elements in nums
plus the current element nums[i]
.
For example, the sequential prefix sum of [1,2,3]
is [1, 1+2, 1+2+3]
which is [1, 3, 6]
.
Given this, find the smallest integer greater than the maximum element in this sequential prefix sum array that does not appear in the array nums
.
nums
consists of positive integers.nums
.nums
is not necessarily sorted.nums
array and compute the sequential prefix sum.nums
. Increment the candidate until we find a missing integer.def smallest_missing_integer(nums):
# Step 1: Compute Sequential Prefix Sum
seq_prefix_sums = []
current_sum = 0
for num in nums:
current_sum += num
seq_prefix_sums.append(current_sum)
# Step 2: Find the maximum value in the prefix sums
max_prefix_sum = max(seq_prefix_sums)
# Step 3: Find the smallest missing integer greater than max_prefix_sum
candidate = max_prefix_sum + 1
while candidate in nums:
candidate += 1
return candidate
# Example usage:
nums = [1, 2, 3]
print(smallest_missing_integer(nums)) # Output should be the smallest missing integer > 6 not in [1, 2, 3]
The time complexity can be broken down as follows:
nums
array once.nums
.Therefore, the overall time complexity is O(n).
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?