BudiBadu Logo
00:00

Search Insert Position

Binary Search Easy 1 views

You are given a list of distinct integers arranged in ascending order and a single target number. Your task is to determine the position of this target within the list.

If the target number is already present, return its index in the list. If it is not present, determine the position where the number should be inserted so that the list remains sorted in ascending order. The insertion position is defined as the index where the target would appear if it were added to the list.

This problem asks you to think carefully about ordered data and how elements relate to one another in a sequence. Pay close attention to cases where the target is smaller than all elements, larger than all elements, or falls between existing values. The goal is to identify the correct position based on the target’s relative order among the numbers.

Example 1:

Input: nums = [1,3,5,6], target = 5
Output: 2
Explanation: The number 5 already exists in the list and is located at index 2.

Example 2:

Input: nums = [1,3,5,6], target = 2
Output: 1
Explanation: The target 2 is not in the list but would fit between 1 and 3, so its position would be index 1.

Example 3:

Input: nums = [1,3,5,6], target = 7
Output: 4
Explanation: The target 7 is greater than all elements, so it should appear at the end, which corresponds to index 4.

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.

BudiBadu Logo

Search Insert Position

Binary Search Easy 1 views

You are given a list of distinct integers arranged in ascending order and a single target number. Your task is to determine the position of this target within the list.

If the target number is already present, return its index in the list. If it is not present, determine the position where the number should be inserted so that the list remains sorted in ascending order. The insertion position is defined as the index where the target would appear if it were added to the list.

This problem asks you to think carefully about ordered data and how elements relate to one another in a sequence. Pay close attention to cases where the target is smaller than all elements, larger than all elements, or falls between existing values. The goal is to identify the correct position based on the target’s relative order among the numbers.

Example 1:

Input: nums = [1,3,5,6], target = 5
Output: 2
Explanation: The number 5 already exists in the list and is located at index 2.

Example 2:

Input: nums = [1,3,5,6], target = 2
Output: 1
Explanation: The target 2 is not in the list but would fit between 1 and 3, so its position would be index 1.

Example 3:

Input: nums = [1,3,5,6], target = 7
Output: 4
Explanation: The target 7 is greater than all elements, so it should appear at the end, which corresponds to index 4.

00:00
Loading editor...
Test Results

Run your code to see test results

Click the Submit button to execute your solution

Related Problems

No related problems found

Comments (0)

Join the Discussion

Share your thoughts, ask questions, or help others with this problem.