AlgoFlow
beginnerSearching

Search insert position

Given a sorted array of distinct integers and a target, return the index where the target is found. If it is absent, return the index where it would be inserted to keep the array sorted.

Examples

input nums = [1, 3, 5, 7], target = 5

output 2

The target is present at index 2.

input nums = [1, 3, 5, 7], target = 6

output 3

6 belongs between 5 and 7, which is index 3.

Hints

Try the problem first. Hints are revealed one at a time so you keep thinking.

Solution

The written approach, complexity analysis and follow-ups stay hidden until you ask for them.