Missing Numbers in the Range

You are given an integer array nums of length n, where every element is in the range [1, n].

Your task is to find all the numbers in the range [1, n] that are not present in the array.

Return these missing numbers in an array in any order.

Example 1:

Input: nums = [1, 2, 2, 4]

Output: [3]

Explanation:

Here, n = 4, so the numbers that should be present are 1, 2, 3, 4.

The number 3 does not appear in nums, so the answer is [3].

Example 2:

Input: nums = [2, 2, 3, 1, 5]

Output: [4]

Explanation:

Here, n = 5, so the numbers from 1 to 5 are expected.

The array contains 1, 2, 3, 5, but 4 is missing.

So, the answer is [4].

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1, 1, 2, 4]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == nums.length
  • 1 <= n <= 105
  • 1 <= nums[i] <= n

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums