2996. Smallest Missing Integer Greater Than Sequential Prefix Sum LeetCode Solution

In this guide, you will get 2996. Smallest Missing Integer Greater Than Sequential Prefix Sum LeetCode Solution with the best time and space complexity. The solution to Smallest Missing Integer Greater Than Sequential Prefix Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Smallest Missing Integer Greater Than Sequential Prefix Sum solution in C++
  4. Smallest Missing Integer Greater Than Sequential Prefix Sum solution in Java
  5. Smallest Missing Integer Greater Than Sequential Prefix Sum solution in Python
  6. Additional Resources
2996. Smallest Missing Integer Greater Than Sequential Prefix Sum LeetCode Solution image

Problem Statement of Smallest Missing Integer Greater Than Sequential Prefix Sum

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 <= 50
1 <= nums[i] <= 50

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

2996. Smallest Missing Integer Greater Than Sequential Prefix Sum LeetCode Solution in C++

class Solution {
 public:
  int missingInteger(vector<int>& nums) {
    const unordered_set<int> numsSet{nums.begin(), nums.end()};
    int ans = nums[0];

    for (int i = 1; i < nums.size(); ++i) {
      if (nums[i] != nums[i - 1] + 1)
        break;
      ans += nums[i];
    }

    while (numsSet.contains(ans))
      ++ans;

    return ans;
  }
};
/* code provided by PROGIEZ */

2996. Smallest Missing Integer Greater Than Sequential Prefix Sum LeetCode Solution in Java

class Solution {
  public int missingInteger(int[] nums) {
    Set<Integer> numsSet = Arrays.stream(nums).boxed().collect(Collectors.toSet());
    int ans = nums[0];

    for (int i = 1; i < nums.length; ++i) {
      if (nums[i] != nums[i - 1] + 1)
        break;
      ans += nums[i];
    }

    while (numsSet.contains(ans))
      ++ans;

    return ans;
  }
}
// code provided by PROGIEZ

2996. Smallest Missing Integer Greater Than Sequential Prefix Sum LeetCode Solution in Python

class Solution:
  def missingInteger(self, nums: list[int]) -> int:
    numsSet = set(nums)
    ans = nums[0]

    for i in range(1, len(nums)):
      if nums[i] != nums[i - 1] + 1:
        break
      ans += nums[i]

    while ans in numsSet:
      ans += 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.