Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be inserted.

30:00

Search Insert Position
easy
Topics
Companies

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be inserted.

Example 1:
Input: {"nums":[1,3,5,6],"target":5}
Output: 2
Constraints:
  • 1nums.length1041 \leq \text{nums.length} \leq 10^4

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

  • nums contains distinct values sorted in ascending order.

  • 104target104-10^4 \leq \text{target} \leq 10^4

Input
arr ={"nums":[1,3,5,6],"target":5}

Binary search

Target: 5

LmidR1<0315263
L: 0
mid: 0
R: 3
nums[mid]: 1
Variables
VariableValue
target5
DepthFunction Call
Stack empty
0/3