File: find-all-numbers-disappeared-in-an-array/solution.py

Date: 2026-06-06

Time: 16:33

Purpose

This file solves LeetCode 448 — Find All Numbers Disappeared in an Array. Given an array nums of n integers where each value is in [1, n], it returns all integers in that range that do not appear in the array. The constraint that matters: it must be solvable in O(n) time without extra space (beyond the output).

Key Components

finddisappearednumbers(nums: list[int]) -> list[int]

Single function, single responsibility. Takes the array, returns the missing numbers.

Contract: nums contains values in [1, n] where n = len(nums). The function mutates nums in place as a side effect — callers get back correct results but the input array is destroyed.

Patterns

In-place negation marking — the core technique. Since every value maps to a valid index (val - 1), the array itself becomes a visited-set by flipping signs:

1. Mark phase (lines 8–10): For each value, compute idx = abs(num) - 1 and negate nums[idx] if it's positive. The abs() is critical — values encountered later in the scan may already have been negated by an earlier iteration, so you need the original magnitude.

2. Collect phase (line 12): Any index i where nums[i] is still positive means no value i + 1 existed in the original array.

This is the textbook O(n) time / O(1) space pattern for "find missing/duplicate in [1, n]" problems. The alternative approaches — hash set (O(n) space) or sorting (O(n log n) time) — are strictly worse on the constraint axis this problem tests.

Dependencies

Imports: None. Pure algorithmic code with no dependencies.

Imported by: The test_solution.py in the same directory. The "Imported By" list in the prompt is misleading — those are unrelated test files across the repo that happen to share a common test harness import pattern, not actual consumers of this function.

Flow


Input: [4, 3, 2, 7, 8, 2, 3, 1]    (n=8, missing: 5 and 6)

Mark phase — iterate and negate at (abs(val) - 1):
  4 → negate idx 3:  [4, 3, 2, -7, 8, 2, 3, 1]
  3 → negate idx 2:  [4, 3, -2, -7, 8, 2, 3, 1]
  2 → negate idx 1:  [4, -3, -2, -7, 8, 2, 3, 1]
  7 → negate idx 6:  [4, -3, -2, -7, 8, 2, -3, 1]
  8 → negate idx 7:  [4, -3, -2, -7, 8, 2, -3, -1]
  2 → idx 1 already negative, skip
  3 → idx 2 already negative, skip
  1 → negate idx 0:  [-4, -3, -2, -7, 8, 2, -3, -1]

Collect phase — positive positions are 4 and 5 (0-indexed):
  → return [5, 6]

Invariants

Error Handling

None. The function assumes valid input per the problem constraints. Values outside [1, n] would cause an IndexError; non-integer input would fail at abs(). This is appropriate for a LeetCode solution where input validity is guaranteed.

Topics to Explore

Beliefs