Given an array **a[]** and an integer **N**, the task is to find all natural numbers from the range **[1, N]** that are nor present in the given array.

**Examples:**

Input:N = 5, a[] = {1, 2, 4, 5}Output:3Explanation:3 is the only integer from the range [1, 5] that is not present in the array.

Input:N = 10, a[] = {1, 3, 4, 6, 8, 10}Output:2 5 7 9

**Naive Approach:** The simplest approach to solve this problem is to traverse the range [1, N] and for each number from range, traverse the array and check if it is present in the array or not. **Time Complexity:** O(N * len), where **len** denotes the length of the array. **Auxiliary Space:** O(1)

**Efficient Approach:** The above approach can be optimized using HashSet. Traverse the given array and insert all array elements into the HashSet. Then, traverse the range [1, N] and for each element, check if it is present in the **HashSet** or not using contains() method, to compute search in O(1) complexity. **Time Complexity:** O(N) **Auxiliary Space:** O(N)

**Alternate Approach:** The given problem can be solved using BitSet in C++. Follow the steps below to solve the problem:

- Initialize a BitSet variable,
**bset**with**N**as length. - For each array element, set its bit to false, using
**bset.set(arr[i]-1, 0)**, where it sets the bit at position**arr[i] – 1**to**0**. - Now, iterate from
**bset._Find_first()**to**bset.size() – 1**using a variable, say**i**. - Print
**i + 1**and set**bset._Find_next()**.

Below is the implementation of the above approach.

## C++

`// CPP program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find positive integers` `// from 1 to N that are not present in the array` `void` `findMissingNumbers(` `int` `arr[], ` `int` `len)` `{` ` ` `const` `int` `M = 15;` ` ` `// Declare bitset` ` ` `bitset<M> bset;` ` ` `// Iterate from 0 to M - 1` ` ` `for` `(` `int` `i = 0; i < M; i++) {` ` ` `bset.set(i);` ` ` `}` ` ` `// Iterate from 0 to len - 1` ` ` `for` `(` `int` `i = 0; i < len; i++) {` ` ` `bset.set(arr[i] - 1, 0);` ` ` `}` ` ` `// Iterate from bset._Find_first()` ` ` `// to bset.size() - 1` ` ` `for` `(` `int` `i = bset._Find_first();` ` ` `i < bset.size();` ` ` `i = bset._Find_next(i)) {` ` ` `if` `(i + 1 > len)` ` ` `break` `;` ` ` `cout << i + 1 << endl;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 1, 2, 4, 6, 8, 9 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `findMissingNumbers(arr, n);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.util.*;` `class` `GFG` `{` ` ` `// Function to find positive integers` ` ` `// from 1 to N that are not present in the array` ` ` `static` `void` `findMissingNumbers(` `int` `[] arr, ` `int` `len)` ` ` `{` ` ` `int` `M = ` `15` `;` ` ` `// Declare bitset` ` ` `BitSet bset = ` `new` `BitSet(M);` ` ` `// Iterate from 0 to M - 1` ` ` `for` `(` `int` `i = ` `0` `; i < M; i++)` ` ` `{` ` ` `bset.set(i);` ` ` `}` ` ` `// Iterate from 0 to len - 1` ` ` `for` `(` `int` `i = ` `0` `; i < len; i++)` ` ` `{` ` ` `bset.set(arr[i] - ` `1` `, ` `false` `);` ` ` `}` ` ` `// Iterate from bset._Find_first()` ` ` `// to bset.size() - 1` ` ` `for` `(` `int` `i = bset.nextSetBit(` `0` `); i >= ` `0` `;` ` ` `i = bset.nextSetBit(i + ` `1` `))` ` ` `{` ` ` `if` `(i + ` `1` `> len)` ` ` `break` `;` ` ` `System.out.println(i + ` `1` `);` ` ` `}` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `[] arr = ` `new` `int` `[] { ` `1` `, ` `2` `, ` `4` `, ` `6` `, ` `8` `, ` `9` `};` ` ` `int` `n = arr.length;` ` ` `findMissingNumbers(arr, n);` ` ` `}` `}` `// This code is contributed by Dharanendra L V` |

**Output:**

3 5

**Time Complexity:** O(N)**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.