35. Search Insert Position

Posted on Mar 7, 2025

Implementation

class Solution {
    fun searchInsert(nums: IntArray, target: Int): Int {
        var (l, r) = 0 to nums.size
        var m = nums.size / 2

        while (r - l > 1) {
            if (nums[m] == target) return m
            if (nums[m] > target) r = m else l = m
            m = (l + r) / 2
        }

        return if (target <= nums[l]) l else r
    }
}