35. Search Insert Position

Practice Search Insert Position for free on NudgeCode: Easy, Binary Search pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

You are given a sorted array nums of distinct integers and a value target. Return the index of target if it is present; otherwise return the index where it would go to keep the array sorted.

Aim for O(log n) time.

Example

nums = [1,3,5,6], target = 2  ->  1

Constraints