Find All Numbers Disappeared In An Array

To solve this coding challenge, we need to identify and return all the numbers in the range [1, n] that are missing from the given array
nums
. The array
nums
has a length of
n
, and every element in
nums
is an integer within the range [1, n]. We need to find a way to achieve this with O(n) runtime without using extra space, although the returned list does not count as extra space per the problem constraints.

Explanation

Here's a detailed breakdown of how we can approach this problem:
  1. Understand the Problem Constraints :
    • We have an array
      nums
      of length
      n
      .
    • Every element in
      nums
      lies between 1 and
      n
      (inclusive).
  2. Objective :
    • Identify all the integers from 1 to
      n
      that are not present in the array
      nums
      .
    • Return these missing integers in a list.
  3. Optimal Strategy :
    • We need to achieve this in O(n) runtime and with constant extra space.
    • A classic approach to achieve O(n) runtime involves modifying the original array in place to mark the presence of elements.
  4. Marking Presence :
    • For each element in the array, the index corresponding to the value (reduced by 1) can be used to mark its presence.
    • To avoid using extra space, we can mark an element as seen by making the value at the corresponding index negative (only if it’s not already negative).
    • This marks the index
      i
      as visited meaning value
      i+1
      is present in the array.
  5. Identifying Missing Numbers :
    • After marking all present elements, any index
      i
      that has a positive value indicates that the number
      i+1
      is missing from the array.
  6. Implementation Steps :
    • Iterate over each element in the array and use its absolute value to determine the index to mark as visited.
    • Iterate over the array again to collect all indices that have positive values; these indices correspond to missing numbers.

    Pseudocode with Comments

                                                
    # Step 1: Define the function to find disappeared numbers
    function findDisappearedNumbers(nums):
    # Get the length of the input array
    array_length = length(nums)
    
    # Step 2: Mark presence of each number
    for each element in nums:
    # Calculate the corresponding index
    corresponding_index = absolute_value(element) - 1
    
    # Mark the presence by making the value at corresponding_index negative
    if nums[corresponding_index] > 0:
    nums[corresponding_index] = -nums[corresponding_index]
    
    # Step 3: Collect the missing numbers
    missing_numbers = []
    for i in range(array_length):
    # If the value at index i is positive, it means number i+1 is missing
    if nums[i] > 0:
    missing_numbers.append(i + 1)
    
    # Step 4: Return the list of missing numbers
    return missing_numbers
    
                                            

    Detailed Steps in Pseudocode

  7. Initialization :
    • Define a function
      findDisappearedNumbers
      that takes an array
      nums
      as input.
    • Calculate the length of
      nums
      and store it in
      array_length
      .
  8. Marking Phase :
    • Iterate over each element in
      nums
      .
    • For each element, compute
      corresponding_index
      as
      abs(element) - 1
      .
    • Check if
      nums[corresponding_index]
      is positive.
    • If it is positive, negate the value at
      nums[corresponding_index]
      to mark it as visited.
  9. Collection Phase :
    • Initialize an empty list
      missing_numbers
      to collect results.
    • Iterate over the range from 0 to
      array_length - 1
      .
    • For each index
      i
      , check if
      nums[i]
      is positive.
    • If
      nums[i]
      is positive, append
      i + 1
      to
      missing_numbers
      .
  10. Final Output :
    • Return the
      missing_numbers
      list containing all the integers from 1 to
      n
      that are not present in
      nums
      .
This approach ensures that we modify the input array in place and collect the results efficiently, respecting both the time complexity and space constraints mentioned in the problem.