LeetCode – 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 if it were inserted in order. You may assume no duplicates in the array.

Here are few examples.

[1,3,5,6], 5 -> 2
[1,3,5,6], 2 -> 1
[1,3,5,6], 7 -> 4
[1,3,5,6], 0 -> 0

Solution 1

Naively, we can just iterate the array and compare target with ith and (i+1)th element. Time complexity is O(n)

public class Solution {
    public int searchInsert(int[] A, int target) {
        if(A==null) return 0;
        if(target <= A[0]) return 0;
        for(int i=0; i<A.length-1; i++){
            if(target > A[i] && target <= A[i+1]){
                return i+1;
        return A.length;

Solution 2

This also looks like a binary search problem. We should try to make the complexity to be O(log(n)).

public class Solution {
    public int searchInsert(int[] A, int target) {
            return 0;
        return searchInsert(A,target,0,A.length-1);
    public int searchInsert(int[] A, int target, int start, int end){
        int mid=(start+end)/2;
            return mid;
        else if(target<A[mid]) 
            return start<mid?searchInsert(A,target,start,mid-1):start;
            return end>mid?searchInsert(A,target,mid+1,end):(end+1);
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Swapnil Kumar

    Can some one review my code –
    public int searchInsert(int[] nums, int target) {
    int low = 0;
    int high = nums.length – 1;

    while(low <= high){
    int mid = (high + low)/2 ;
    if(nums[mid] == target){
    return mid;
    else if(target nums[high]){
    return high + 1;
    return high;
    }else if(low > high){
    return low;

    return -1;

  • Sureshkumar T

    for midpoint method the complexity is O(ln(n)) where n is size of the array..

  • Guest

    For the binary search method the runtime is incorrectly stated to O(nlogn) it should be O(log n)

  • Kartrace

    the mid can be calculated as mid = start + (end-start)/2 to avoid start+end integer overflow if both start and end are big numbers.