Given an array containing numbers from 1 to n with some values repeated, find which numbers in that range do not appear.
Problem
You are given an integer array nums of length n where each value is expected to be in the range 1 to n inclusive.
Some numbers may appear twice, and some numbers from that range may be missing. Your task is to return all missing numbers in ascending order.
Goal
Identify every integer in [1, n] that does not occur in the array.
Notes
- The answer should contain each missing number exactly once.
- The array may contain duplicates.
- You may solve this using extra space, or by reusing the input array if you prefer.
Input Format
- An integer array
numsof lengthn. - Each
nums[i]is intended to be in the range1..n.
Interpretation
The valid candidate numbers are exactly the integers from 1 through n.
Output Format
- Return a list of all numbers in the range
1..nthat do not appear innums. - The numbers should be returned in ascending order.
Constraints
1 <= n <= $10^{5}$1 <= nums[i] <= n- The array may contain duplicates
- Each missing number should appear once in the output
Example 1
Input
nums = [4,3,2,7,8,2,3,1]
Output
[5,6]
Explanation
The numbers from 1 to 8 are {1,2,3,4,5,6,7,8}. The values 5 and 6 never appear in the array.
Example 2
Input
nums = [1,1]
Output
[2]
Explanation
The range is {1,2}. Only 1 appears, so 2 is missing.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.