Search⌘ K
AI Features

Solution: Search Insert Position

Understand how to apply a modified binary search algorithm to determine the index of a target value in a sorted array or the position where it should be inserted. This lesson helps you master the approach that runs in logarithmic time and operates with constant space, improving your problem-solving efficiency in coding interviews.

Statement

Given a sorted array of distinct integers, nums, and an integer, target, return the index of target if it exists in the array.
If the target is not present, return the index where it should be inserted to maintain the sorted order.

Your algorithm must run in the ...