Find the smallest positive number missing from an unsorted array | Set 2 | Function to find smallest positive missing number . ; to store current array element ; to store next array element in current traversal ; if value is negative or greater than array size , then it cannot be marked in array . So move to next element . ; traverse the array until we reach at an element which is already marked or which could not be marked . ; find first array index which is not marked which is also the smallest positive missing number . ; if all indices are marked , then smallest missing positive number is array_size + 1. ; Driver code
< ? php function findMissingNo ( $ arr , $ n ) { $ val ; $ nextval ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] <= 0 $ arr [ $ i ] > $ n ) continue ; $ val = $ arr [ $ i ] ; while ( $ arr [ $ val - 1 ] != $ val ) { $ nextval = $ arr [ $ val - 1 ] ; $ arr [ $ val - 1 ] = $ val ; $ val = $ nextval ; if ( $ val <= 0 $ val > $ n ) break ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] != $ i + 1 ) { return $ i + 1 ; } } return $ n + 1 ; } $ arr = array ( 2 , 3 , 7 , 6 , 8 , -1 , -10 , 15 ) ; $ arr_size = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ missing = findMissingNo ( $ arr , $ arr_size ) ; echo " The ▁ smallest ▁ positive ▁ " . " missing ▁ number ▁ is ▁ " , $ missing ; ? >