Math Fundamentals18 sections · 814 units
Open in Course

Find Disappeared - The Idea

In-place marking

Use the array itself as a boolean flag system. For each number xx you encounter, mark index x1x - 1 by negating the value there. After processing, positive values indicate missing numbers.

When you see nums[i], compute index = abs(nums[i]) - 1. Then set nums[index] = -abs(nums[index]). The absolute value handles cases where the value is already negative.

After marking, iterate through the array. If nums[i] > 0, then i+1i + 1 is missing. This boolean check (positive vs. negative) tracks presence without extra space.