k | Function to find the sum of minimum of all subarrays ; Insert all the elements in a set ; Find the maximum and minimum element ; Traverse from the minimum to maximum element ; Check if " i " is missing ; Check if it is kth missing ; If no kth element is missing ; Driver code
function findKth ( arr , n , k ) { var missing = new Set ( ) ; var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) missing . add ( arr [ i ] ) ; var maxm = arr . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var minm = arr . reduce ( ( a , b ) => Math . min ( a , b ) ) ; for ( var i = minm + 1 ; i < maxm ; i ++ ) { if ( ! missing . has ( i ) ) count ++ ; if ( count == k ) return i ; } return - 1 ; } var arr = [ 2 , 10 , 9 , 4 ] ; var n = arr . length ; var k = 5 ; document . write ( findKth ( arr , n , k ) ) ;