Math Fundamentals18 sections · 814 units
Open in Course

Problem - Search Insert Position (Second problem)

LeetCode 35

Given a sorted array and a target value, return the index where the target is found. If not found, return the index where it would be inserted to keep the array sorted.

For example, given [1,3,5,6][1, 3, 5, 6] and target 55, return 22. Given target 22, return 11 (insert between 11 and 33).

The sorted array is your clue. You can use binary search to find the position in O(logn)O(\log n) time instead of scanning the entire array.