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
< ? php function printKMissing ( $ arr , $ n , $ k ) { sort ( $ arr ) ; sort ( $ arr , $ n ) ; $ i = 0 ; while ( $ i < $ n && $ arr [ $ i ] <= 0 ) $ i ++ ; $ count = 0 ; $ curr = 1 ; while ( $ count < $ k && $ i < $ n ) { if ( $ arr [ $ i ] != $ curr ) { echo $ curr , " " ; $ count ++ ; } else $ i ++ ; $ curr ++ ; } while ( $ count < $ k ) { echo $ curr , " " ; $ curr ++ ; $ count ++ ; } } $ arr = array ( 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; $ k = 3 ; printKMissing ( $ arr , $ n , $ k ) ; ? >