 All Problems
Search Insert Position
easy
binary search
array
amazon
google
microsoft

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be inserted in order.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: 1 3 5 6
Target: 5
Output: 2

Example 2:

Input: 1 3 5 6
Target: 2
Output: 1

Example 3:

Input: 1 3 5 6
Target: 7
Output: 4

Input format: First line: sorted space-separated array. Second line: target.

Output format: Index where target is or should be inserted.

Run to check your code against the sample cases, or submit to run every case