Find first k natural numbers missing in given array | C ++ code for the above approach ; Program to print first k missing number ; Creating a hashmap ; Iterate over array ; Iterate to find missing element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printmissingk ( int arr [ ] , int n , int k ) { map < int , int > d ; for ( int i = 0 ; i < n ; i ++ ) d [ arr [ i ] ] = arr [ i ] ; int cnt = 1 ; int fl = 0 ; for ( int i = 0 ; i < ( n + k ) ; i ++ ) { if ( d . find ( cnt ) == d . end ( ) ) { fl += 1 ; cout << cnt << " ▁ " ; if ( fl == k ) break ; } cnt += 1 ; } } int main ( ) { int arr [ ] = { 1 , 4 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; ; printmissingk ( arr , n , k ) ; }