Positive integers up to N that are not present in given Array
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: 3
Explanation: 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 |
Python3
# Python 3 program for the above approach # Function to find positive integers # from 1 to N that are not present in the array def findMissingNumbers(arr, n): M = 15 # Declare bitset bset = [ 0 ] * M # Iterate from 0 to M - 1 for i in range (M): bset[i] = i # Iterate from 0 to n - 1 for i in range (n): bset[arr[i] - 1 ] = 0 bset = [i for i in bset if i ! = 0 ] # Iterate from bset._Find_first() # to bset.size() - 1 for i in range ( len (bset)): if (bset[i] + 1 > n): break print (bset[i] + 1 ) # Driver Code if __name__ = = "__main__" : arr = [ 1 , 2 , 4 , 6 , 8 , 9 ] n = len (arr) findMissingNumbers(arr, n) # This code is contributed by ukasp. |
C#
// C# program for the above approach using System; using System.Collections; 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 int [] bset = new int [M]; // Iterate from 0 to M - 1 for ( int i = 0; i < M; i++) { bset[i] = i; } // Iterate from 0 to len - 1 for ( int i = 0; i < len; i++) { bset[arr[i] - 1] = 0; } ArrayList temp = new ArrayList(); foreach ( int x in bset){ if (x != 0){ temp.Add(x); } } // Iterate from bset._Find_first() // to bset.size() - 1 for ( int i = 0; i < temp.Count; i++) { if (( int )temp[i] + 1 > len){ break ; } Console.WriteLine(( int )temp[i] + 1); } } // Driver Code public static void Main() { int [] arr = new int [] { 1, 2, 4, 6, 8, 9 }; int n = arr.Length; findMissingNumbers(arr, n); } } // This code is contributed by Saurabh Jaiswal |
Javascript
<script> // JavaScript program for the above approach // Function to find positive integers // from 1 to N that are not present in the array function findMissingNumbers(arr, n){ let M = 15 // Declare bitset let bset = new Array(M).fill(0) // Iterate from 0 to M - 1 for (let i=0;i<M;i++) bset[i] = i // Iterate from 0 to n - 1 for (let i=0;i<n;i++) bset[arr[i] - 1] = 0 bset = bset.filter((i)=>i != 0) // Iterate from bset._Find_first() // to bset.size() - 1 for (let i = 0; i < bset.length; i++) { if (bset[i] + 1 > n) break document.write(bset[i] + 1, "</br>" ) } } // Driver Code let arr = [1, 2, 4, 6, 8, 9] let n = arr.length findMissingNumbers(arr, n) // This code is contributed by shinjanpatra </script> |
3 5
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...