leetcode

Find All Numbers Disappeared in an Array - Link

Question Description

Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.


Constraints


Solution 1: Brute Force Approach (O(n²) time)

Approach

For each number from 1 to n, iterate through the entire array to check if it exists. If not found after checking all elements, add it to the result list. This is the most straightforward but least efficient approach.

Dry Run

Example Input: nums = [4,3,2,7,8,2,3,1]

Step-by-step execution:

Final Answer = [5, 6]

class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= nums.length; i++) {
            boolean found = false;
            for (int j = 0; j < nums.length; j++) {
                if (nums[j] == i) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                list.add(i);
            }
        }
        return list;
    }
}

Time and Space Complexity


Solution 2: HashSet Approach (O(n) space)

Approach

Use a HashSet to store all numbers present in the array. Then iterate from 1 to n and check which numbers are missing from the set. This provides O(1) lookup time for checking existence.

Dry Run

Example Input: nums = [4,3,2,7,8,2,3,1]

Step-by-step execution:

Final Answer = [5, 6]

class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> list = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        for (int ele : nums) {
            set.add(ele);
        }
        for (int i = 1; i <= nums.length; i++) {
            if (!set.contains(i)) {
                list.add(i);
            }
        }
        return list;
    }
}

Time and Space Complexity


Solution 3: In-place Marking (Optimal - O(1) space)

Approach

Use the array itself as a hash table by marking visited indices. For each number, mark the index (num - 1) as negative. After processing, indices with positive values indicate missing numbers. This approach satisfies the follow-up requirement for O(1) extra space.

Dry Run

Example Input: nums = [4,3,2,7,8,2,3,1]

Step-by-step execution:

Check positive indices: i=4 (nums[4]=8 > 0) → missing number 5, i=5 (nums[5]=2 > 0) → missing number 6

Final Answer = [5, 6]

class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> list = new ArrayList<>();

        // Mark visited indices as negative
        for (int i = 0; i < nums.length; i++) {
            int index = Math.abs(nums[i]) - 1;
            if (nums[index] > 0) {
                nums[index] = -nums[index];
            }
        }

        // Indices with positive values are missing numbers
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                list.add(i + 1);
            }
        }

        return list;
    }
}

Time and Space Complexity