Find first k natural numbers missing in given array | Java 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
import java . io . * ; import java . util . HashMap ; class GFG { static void printmissingk ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > d = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) d . put ( arr [ i ] , arr [ i ] ) ; int cnt = 1 ; int fl = 0 ; for ( int i = 0 ; i < ( n + k ) ; i ++ ) { if ( ! d . containsKey ( cnt ) ) { fl += 1 ; System . out . print ( cnt + " ▁ " ) ; if ( fl == k ) break ; } cnt += 1 ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 3 } ; int n = arr . length ; int k = 3 ; printmissingk ( arr , n , k ) ; } }