Search⌘ K
AI Features

Search Insert Position

Explore how to use modified binary search to efficiently find the index of a target value or the correct insertion point in a sorted array of distinct integers. This lesson helps you develop a precise understanding of problem constraints and implement optimal solutions in C# with logarithmic time complexity.

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 O(logn)O(\log n) time.

Constraints:

  • 1 ...