Tap here to switch tabs
Problem
Submissions

Problem: Search Insert Position

Search Insert Position gives you a sorted array of distinct integers and a target. Return the target's index, or its insertion position, in logarithmic time.

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(log⁡n)O(\log n) time.

Constraints:

  • 1≤1 \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

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Search Insert Position

Search Insert Position gives you a sorted array of distinct integers and a target. Return the target's index, or its insertion position, in logarithmic time.

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(log⁡n)O(\log n) time.

Constraints:

  • 1≤1 \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