Math Fundamentals18 sections · 814 units
Open in Course

Find Disappeared - Implementation

The code

Here's the complete solution:


function findDisappearedNumbers(nums)
    // Mark presence using negative values
    for i from 0 to length of nums - 1
        index := abs(nums[i]) - 1
        if nums[index] > 0 then
            nums[index] := -nums[index]
    
    // Collect missing numbers
    result := empty list
    for i from 0 to length of nums - 1
        if nums[i] > 0 then
            result.append(i + 1)
    
    return result

The boolean condition nums[index] > 0 checks if the index has already been marked. The second nums[i] > 0 check identifies missing numbers. Both use sign as a boolean flag.