Find four missing numbers in an array containing elements from 1 to N in Python

Finding missing numbers in an array is a common programming problem. When we have an array of distinct numbers from range [1, N] with size (N-4), exactly four numbers are missing. This problem can be solved efficiently using array manipulation techniques.

Problem Understanding

Given an array of distinct numbers where:

  • Each number lies in range [1, N]
  • Array size is (N-4)
  • No element is repeated
  • We need to find 4 missing numbers in sorted order

For example, if A = [2, 8, 4, 13, 6, 11, 9, 5, 10], then N = 13 and the missing numbers are [1, 3, 7, 12].

Algorithm Steps

The solution uses a marking technique:

  • Create a temporary array of size 4 to handle numbers greater than array length
  • Mark present numbers by making corresponding positions negative
  • Identify missing numbers from unmarked positions

Implementation

def find_missing_nums(A):
    temp_arr = [0] * 4
    
    # Mark present numbers
    for i in range(len(A)):
        temp = abs(A[i])
        if temp <= len(A):
            # Mark numbers within array bounds
            A[temp - 1] = A[temp - 1] * (-1)
        elif temp > len(A):
            # Mark numbers beyond array bounds in temp_arr
            if temp % len(A):
                temp_arr[temp % len(A) - 1] = -1
            else:
                temp_arr[(temp % len(A)) + len(A) - 1] = -1
    
    # Find missing numbers in range [1, len(A)]
    missing_numbers = []
    for i in range(len(A)):
        if A[i] > 0:
            missing_numbers.append(i + 1)
    
    # Find missing numbers in range [len(A)+1, len(A)+4]
    for i in range(len(temp_arr)):
        if temp_arr[i] >= 0:
            missing_numbers.append(len(A) + i + 1)
    
    return missing_numbers

# Example usage
A = [2, 8, 4, 13, 6, 11, 9, 5, 10]
result = find_missing_nums(A.copy())  # Use copy to preserve original array
print("Missing numbers:", result)
Missing numbers: [1, 3, 7, 12]

How It Works

The algorithm works in two phases:

Phase 1: Marking Present Numbers

  • For numbers ? array length: Mark by making A[number-1] negative
  • For numbers > array length: Mark in temp_arr using modulo operation

Phase 2: Finding Missing Numbers

  • Positive values in A indicate missing numbers in range [1, len(A)]
  • Non-negative values in temp_arr indicate missing numbers in range [len(A)+1, len(A)+4]

Alternative Approach Using Set

A simpler approach using Python's set operations:

def find_missing_simple(A):
    n = len(A) + 4  # Total range is 1 to n
    complete_set = set(range(1, n + 1))
    array_set = set(A)
    missing = sorted(list(complete_set - array_set))
    return missing

# Example usage
A = [2, 8, 4, 13, 6, 11, 9, 5, 10]
result = find_missing_simple(A)
print("Missing numbers:", result)
Missing numbers: [1, 3, 7, 12]

Comparison

Approach Time Complexity Space Complexity Readability
Array Marking O(n) O(1) Complex
Set Difference O(n) O(n) Simple

Conclusion

The array marking technique provides a space-efficient solution with O(1) extra space. For better readability, the set-based approach is simpler and more intuitive, though it uses O(n) extra space.

Updated on: 2026-03-25T09:38:19+05:30

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements