Find first k natural numbers missing in given array | Prints first k natural numbers in arr [ 0. . n - 1 ] ; Find first positive number ; Now find missing numbers between array elements ; Find missing numbers after maximum . ; Driver code
function printKMissing ( arr , n , k ) { arr . sort ( ( a , b ) => a - b ) ; let i = 0 ; while ( i < n && arr [ i ] <= 0 ) i ++ ; let count = 0 , curr = 1 ; while ( count < k && i < n ) { if ( arr [ i ] != curr ) { document . write ( curr + " " ) ; count ++ ; } else i ++ ; curr ++ ; } while ( count < k ) { document . write ( curr , " " ) ; curr ++ ; count ++ ; } } let arr = new Array ( 2 , 3 , 4 ) ; let n = arr . length ; let k = 3 ; printKMissing ( arr , n , k ) ;