text: k | Function to find k - th missing element ; interating over the array ; Check if i - th and ( i + 1 ) - th element are not consecutive ; Save their difference ; Check for difference and given k ; If found ; Input array ; k - th missing element to be found in the array ; Calling function to find missing element
code: function missingK ( a , k , n ) { let difference = 0 , ans = 0 , count = k ; let flag = false ; for ( let i = 0 ; i < n - 1 ; i ++ ) { difference = 0 ; if ( ( a [ i ] + 1 ) != a [ i + 1 ] ) { difference += ( a [ i + 1 ] - a [ i ] ) - 1 ; if ( difference >= count ) { ans = a [ i ] + count ; flag = true ; break ; } else count -= difference ; } } if ( flag ) return ans ; else return - 1 ; } let a = [ 1 , 5 , 11 , 19 ] ; let k = 11 ; let n = a . length ; let missing = missingK ( a , k , n ) ; document . write ( missing ) ;