Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: First Missing Positive

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "41" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums. You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space. Example 1: Input: nums = [1,2,0] Output: 3 Explanation: The numbers in the range [1,2] are all in the array. Example 2: Input: nums = [3,4,-1,1] Output: 2 Explanation: 1 is in the array but 2 is missing. Example 3: Input: nums = [7,8,9,11,12] Output: 1 Explanation: The smallest positive integer 1 is missing. Constraints: 1 <= nums.length <= 105 -231 <= nums[i] <= 231 - 1

Explanation

Here's the approach and Python code to solve this problem efficiently:

  • Utilize the array as a hash table: Since we're looking for the smallest positive integer, we can use the array indices (1 to n) to track the presence of numbers within the range [1, n].
  • Rearrange the array: Iterate through the array. If an element nums[i] is within the range [1, n], move it to its correct position (i.e., nums[nums[i] - 1] = nums[i]). Handle duplicates and out-of-range values gracefully.
  • Find the missing positive: After rearrangement, iterate through the array again. The first index i where nums[i] != i + 1 indicates that i + 1 is the smallest missing positive. If all numbers from 1 to n are present, then the answer is n + 1.

  • Runtime and Storage Complexity: O(n) time, O(1) auxiliary space.

Code

    def firstMissingPositive(nums):
    """
    Finds the smallest missing positive integer in an array.

    Args:
        nums: A list of integers.

    Returns:
        The smallest missing positive integer.
    """
    n = len(nums)

    # Basic idea:
    # 1. Check if 1 is present in the array. If not, you're done and 1 is the answer.
    if 1 not in nums:
        return 1

    # 2. Replace negative numbers, zeros,
    # and numbers larger than n by 1.
    # After this conversion, nums will contain
    # only positive numbers.
    for i in range(n):
        if nums[i] <= 0 or nums[i] > n:
            nums[i] = 1

    # 3. Use the index as a hash key and number sign as a presence detector.
    # For example, if nums[1] is negative, that means that the number `1`
    # is present in the array.
    # If nums[2] is positive, the number 2 is missing.
    for i in range(n):
        a = abs(nums[i])
        # If you meet number a in the array, change the sign of a-th element.
        # Be careful with duplicates: do it only once.
        if a == n:
            nums[0] = - abs(nums[0])
        else:
            nums[a] = - abs(nums[a])

    # 4. Now the index of the first positive number
    # is equal to the first missing positive.
    for i in range(1, n):
        if nums[i] > 0:
            return i

    if nums[0] > 0:
        return n

    return n + 1

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: First Missing Positive