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 if it were inserted in order. You must write an algorithm with O(log n) runtime complexity.
Example 1
Input: nums = [1,3,5,6], target = 5
Output: 2
Explanation: 5 is already in the array, at index 2.
Example 2
Input: nums = [1,3,5,6], target = 2
Output: 1
Explanation: 2 is not in the array. It belongs between 1 and 3, so it would be inserted at index 1.
Example 3
Input: nums = [1,3,5,6], target = 7
Output: 4
Explanation: 7 is larger than every element, so it would be inserted at the end, index 4.
Binary search for the first index whose value is >= target. That index is the answer whether or not the target is present: if present it's the target's position, otherwise it's exactly where the target belongs.
Run checks all cases above. Submit evaluates all test cases.