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
function findMissingNo ( arr , n ) { var val ; var nextval ; for ( var 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 ( var i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != i + 1 ) { return i + 1 ; } } return n + 1 ; } var arr = [ 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 ] ; var arr_size = arr . length ; var missing = findMissingNo ( arr , arr_size ) ; document . write ( " " + missing ) ;