Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1] Output: [5,6]
Example 2:
Input: nums = [1,1] Output: [2]
Constraints:
n == nums.length1 <= n <= 1051 <= nums[i] <= nFollow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.
Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[4,3,2,7,8,2,3,1]
[1,1]
first-missing-positive)find-all-duplicates-in-an-array)find-unique-binary-string)append-k-integers-with-minimal-sum)replace-elements-in-an-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #448: Find All Numbers Disappeared in an Array
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
int n = nums.length;
boolean[] s = new boolean[n + 1];
for (int x : nums) {
s[x] = true;
}
List<Integer> ans = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (!s[i]) {
ans.add(i);
}
}
return ans;
}
}
// Accepted solution for LeetCode #448: Find All Numbers Disappeared in an Array
func findDisappearedNumbers(nums []int) (ans []int) {
n := len(nums)
s := make([]bool, n+1)
for _, x := range nums {
s[x] = true
}
for i := 1; i <= n; i++ {
if !s[i] {
ans = append(ans, i)
}
}
return
}
# Accepted solution for LeetCode #448: Find All Numbers Disappeared in an Array
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
s = set(nums)
return [x for x in range(1, len(nums) + 1) if x not in s]
// Accepted solution for LeetCode #448: Find All Numbers Disappeared in an Array
impl Solution {
pub fn find_disappeared_numbers(nums: Vec<i32>) -> Vec<i32> {
let mut nums = nums;
for i in 0..nums.len() {
let j = nums[i].abs() - 1;
nums[j as usize] = -1 * nums[j as usize].abs();
}
let mut result: Vec<i32> = vec![];
for (index, number) in nums.iter().enumerate() {
if *number > 0 {
result.push((index + 1) as i32);
}
}
result
}
}
// Accepted solution for LeetCode #448: Find All Numbers Disappeared in an Array
function findDisappearedNumbers(nums: number[]): number[] {
const n = nums.length;
const s: boolean[] = new Array(n + 1).fill(false);
for (const x of nums) {
s[x] = true;
}
const ans: number[] = [];
for (let i = 1; i <= n; ++i) {
if (!s[i]) {
ans.push(i);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.