K | Javascript program to find the K - th smallest element after removing some integers from natural number . ; Return the K - th smallest element . ; Making an array , and mark all number as unmarked . ; Marking the number present in the given array . ; If j is unmarked , reduce k by 1. ; If k is 0 return j . ; Driver code
let MAX = 1000000 ; function ksmallest ( arr , n , k ) { let b = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( let j = 1 ; j < MAX ; j ++ ) { if ( b [ j ] != 1 ) { k -- ; } if ( k != 1 ) { return j ; } } return Number . MAX_VALUE ; } let k = 1 ; let arr = [ 1 ] ; let n = arr . length ; document . write ( ksmallest ( arr , n , k ) ) ;