Problem
Ask
Submissions

Problem: Search Insert Position

Medium
30 min
Explore how to apply modified binary search to identify the index of a target value in a sorted array or determine the correct insertion position to maintain order. This lesson enhances your ability to solve search problems efficiently while meeting time complexity requirements.

Statement

Given a sorted array of distinct integers, nums, and an integer, target, return the index of target if it exists in the array.
If the target is not present, return the index where it should be inserted to maintain the sorted order.

Your algorithm must run in the O(logn)O(\log n) time.

Constraints:

  • 11 \leq nums.length 103\leq 10^3

  • 104-10^4 \leq nums[i] 104\leq 10^4

  • nums contains distinct values sorted in ascending order.

  • 104-10^4 \leq target 104\leq 10^4

Problem
Ask
Submissions

Problem: Search Insert Position

Medium
30 min
Explore how to apply modified binary search to identify the index of a target value in a sorted array or determine the correct insertion position to maintain order. This lesson enhances your ability to solve search problems efficiently while meeting time complexity requirements.

Statement

Given a sorted array of distinct integers, nums, and an integer, target, return the index of target if it exists in the array.
If the target is not present, return the index where it should be inserted to maintain the sorted order.

Your algorithm must run in the O(logn)O(\log n) time.

Constraints:

  • 11 \leq nums.length 103\leq 10^3

  • 104-10^4 \leq nums[i] 104\leq 10^4

  • nums contains distinct values sorted in ascending order.

  • 104-10^4 \leq target 104\leq 10^4