Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Search Insert Position

XPChallenge Points: 10
levelLevel: Easy

You are given a sorted array of distinct integers nums and a target value.

·       If target exists in the array, return its index.

·       If not, return the index where target should be inserted to maintain the sorted order.

·       Your algorithm must run in O(log n) time.

Example 1:

Input: nums = [2,4,7,10], target = 7

Output: 2

Explanation:

7 is found at index 2.

Example 2:

Input: nums = [2,4,7,10], target = 5

Output: 2

Explanation:

5 should be inserted at index 2 to maintain order.

Example 3:

Input: nums = [2,4,7,10], target = 12

Output: 4

Explanation:

2 would be placed at the end, index 4.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result