Skip to main content

Command Palette

Search for a command to run...

Smallest Missing Integer Greater Than Sequential Prefix Sum

Published
1 min read
Smallest Missing Integer Greater Than Sequential Prefix Sum
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem: Link | Click Here

class Solution {
    public int missingInteger(int[] nums) {
        // HashSet to store unique numbers from the array
        HashSet<Integer> hs = new HashSet<Integer>();

        // Add all elements from the array to the HashSet to remove duplicates
        for(int e : nums){
            hs.add(e);
        }

        // Initialize the sum with the first number in the array
        int sum = nums[0];

        // Iterate through the array starting from the second element
        for(int i = 1; i < nums.length; i++){
            // If the current number is one more than the previous number
            if(nums[i] == nums[i - 1] + 1){
                sum += nums[i]; // Add the current number to the sum
            } else {
                break; // If there's a gap, exit the loop
            }
        }

        // Store the sum in the 'result' variable
        int result = sum;

        // Find the missing integer by incrementing 'result' until it's not in the HashSet
        while(hs.contains(result)){
            result++;
        }

        // Return the missing integer
        return result;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.