Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 793 Bytes

leetcode_1539.md

File metadata and controls

31 lines (23 loc) · 793 Bytes

LeetCode Problems

1539. Kth Missing Positive Number

class Solution {
    fun findKthPositive(arr: IntArray, k: Int): Int {
        var lowBound = 0
        var highBound = arr.size
        // binary search
        while (lowBound < highBound) {
            val mid = lowBound + (highBound - lowBound) / 2
            // we have to find num in phrase below
            // arr[mid] - (mid + 1) : passed missing-number count in current cursor
            if (arr[mid] - (mid + 1) < k) lowBound = mid + 1
            else highBound = mid
        }
        return lowBound + k
    }
}