InterviewBit Academy is now Scaler!
Learn Tech Skills from Scratch @ Scaler EDGE

Sorted Insert Position


Problem Description

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

You may assume no duplicates in the array.



**Problem Constraints**

1 <= |A| <= 100000

1 <= B <= 109



**Input Format**

First argument is array A.

Second argument is integer B.



**Output Format**

Return an integer, the answer to the problem.



**Example Input**

Input 1:

 A = [1, 3, 5, 6]
B = 5

Input 2:

 A = [1, 3, 5, 6]
B = 2



**Example Output**

Output 1:

 2

Output 2:

 1



**Example Explanation**

Explanation 1:

 5 is found at index 2.

Explanation 2:

 2 will be inserted ar index 1.



NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Sorted Insert Position on Interview Code Editor
Sign Up
to access hints and editorial solutions for Sorted Insert Position
Asked In:

Discussion


Loading...
Click here to start solving coding interview questions