
Search Insert Position (easy)
Problem Statement
Given an array nums containing integers in non-decreasing order and integer target, return the index if the target is found. If not, return the index where it can be placed such that the final array elements are in non-decreasing order.
You must solve the problem in o(logn) time.
Examples
-
Example 1:
- Input: nums = [2,4,6,8,10], target = 1
- Expected Output: 0
- Justification: The target number 1 does not exist in the array but should be inserted at index 0 to keep the array in ascending order.
-
Example 2:
- Input: nums = [1,3,7,9,11], target = 10
- Expected Output: 4
- Justification: The target number 10 is not present in the array. It fits between 9 and 11, making the fourth index (0-based indexing) its rightful position.
-
Example 3:
- Input: nums = [5,8,12], target = 12
- Expected Output: 2
- Justification: The number 12 is present at index 2 in the array.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory