Leetcode 2996. Smallest Missing Integer Greater Than Sequential Prefix Sum
Given an array of positive integers nums, find the smallest missing positive integer greater than the sum of the first i elements for each prefix sum. The result should be the smallest integer that cannot be formed by the sum of the prefix elements plus these integers.
1.1 to the current prefix sum, the next integer we can’t form with the given prefix is the prefix sum plus 1.1.public class SmallestMissingInteger {
public int findSmallestMissing(int[] nums) {
if (nums.length == 0) {
return 1;
}
int prefixSum = 0;
for (int num : nums) {
if (num > prefixSum + 1) {
break;
}
prefixSum += num;
}
return prefixSum + 1;
}
}
// Example usage:
// SmallestMissingInteger solution = new SmallestMissingInteger();
// int result = solution.findSmallestMissing(new int[]{1, 1, 2, 5, 3});
// System.out.println(result); // Output: 13
nums. We only make a single pass through the array.This approach is efficient and leverages a simple scan through the array to solve the problem.
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?