Search Insert Position in Sorted Array Lower Bound Binary Search

Visualization Player

Problem Statement

Given a sorted array of distinct integers and a target number x, your task is to find the insert position of x.

This means you must return the index where x is located in the array. If it is not present, return the index where it should be inserted in order to keep the array sorted.

This problem is also known as finding the lower bound — the first index where an element is greater than or equal to x.

Examples

Input Array Target (x) Insert Position Description
[10, 20, 30, 40, 50] 35 3 35 should be inserted before 40
[10, 20, 30, 40, 50] 20 1 Exact match at index 1
[10, 20, 30, 40, 50] 5 0 Smaller than all elements, inserted at beginning
[10, 20, 30, 40, 50] 55 5 Larger than all elements, inserted at end
[5, 15, 25, 35] 25 2 Exact match at index 2
[5] 2 0 Inserted before the only element
[5] 5 0 Exact match at index 0
[5] 8 1 Inserted after the only element

Solution

Understanding the Problem

Suppose you are given a sorted array, and you want to find the correct position to insert a target number x so that the array remains sorted. This problem is not just about checking if x is present; it's about finding the position where it could be inserted — this is called the lower bound.

For example, if arr = [1, 3, 5, 6] and x = 5, the output should be 2 because 5 is already at index 2.

But if x = 4, then the correct insert position is 2 because it would go between 3 and 5.

Step-by-Step Intuition

Let’s solve this using a technique called binary search, which is perfect for sorted arrays. Instead of checking every position one by one, we’ll divide the array in halves, zero in on the region where x could go, and find the first index where arr[i] ≥ x.

Step 1: Initialize Search Range

We start with two pointers:

  • low = 0 (start of the array)
  • high = n - 1 (end of the array)

We also define ans = n (default answer is the end of array, in case x is greater than all elements).

Step 2: Binary Search Loop

Repeat while low ≤ high:

  • Calculate mid = Math.floor((low + high) / 2)
  • If arr[mid] ≥ x:
    • This index might be the correct insert position, so set ans = mid
    • Move search to the left half: high = mid - 1
  • Else:
    • x is greater than arr[mid], so move right: low = mid + 1

Step 3: Final Answer

After the loop ends, ans will hold the first index where x can be inserted to maintain the sorted order.

Example Walkthrough

Let’s try with arr = [1, 3, 5, 6] and x = 4.

  • low = 0, high = 3
  • mid = 1arr[1] = 3 → 3 < 4 → move right → low = 2
  • mid = 2arr[2] = 5 → 5 ≥ 4 → ans = 2, high = 1
  • Loop ends → Final answer: 2

Handling Edge Cases

  • x is smaller than all elements → e.g., x = 0 → insert at index 0
  • x is greater than all elements → e.g., x = 10 → insert at index arr.length
  • x is equal to an existing value → insert at the first occurrence (lower bound)

Why Binary Search?

Binary search gives us an efficient way to solve this in O(log n) time instead of scanning every element. This is especially useful for large arrays.

Algorithm Steps

  1. Given a sorted array arr of distinct integers and a target value x.
  2. Initialize: low = 0, high = arr.length - 1, ans = arr.length.
  3. While low ≤ high:
  4. → Compute mid = Math.floor((low + high) / 2).
  5. → If arr[mid] ≥ x, update ans = mid and move left: high = mid - 1.
  6. → Else, move right: low = mid + 1.
  7. Return ans as the correct insert position.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#include <stdio.h>

int searchInsertPosition(int arr[], int size, int target) {
  int low = 0, high = size - 1, ans = size;
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (arr[mid] >= target) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  return ans;
}

int main() {
  int arr[] = {1, 3, 5, 6};
  int size = sizeof(arr) / sizeof(arr[0]);
  int target = 2;
  printf("Insert Position: %d\n", searchInsertPosition(arr, size, target));
  return 0;
}

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(1)The target is found at the middle index on the first comparison, requiring only one iteration.
Average CaseO(log n)Binary search halves the search space in each iteration, so on average, it takes log n steps to find the insert position.
Worst CaseO(log n)In the worst case, the entire array must be searched logarithmically to determine where the target should be inserted.

Space Complexity

O(1)

Explanation: The algorithm operates in-place using only a constant amount of memory — just a few integer variables for low, high, mid, and ans.

Problem Statement

Given a sorted array of distinct integers and a target number x, your task is to find the insert position of x.

This means you must return the index where x is located in the array. If it is not present, return the index where it should be inserted in order to keep the array sorted.

This problem is also known as finding the lower bound — the first index where an element is greater than or equal to x.

Examples

Input Array Target (x) Insert Position Description
[10, 20, 30, 40, 50] 35 3 35 should be inserted before 40
[10, 20, 30, 40, 50] 20 1 Exact match at index 1
[10, 20, 30, 40, 50] 5 0 Smaller than all elements, inserted at beginning
[10, 20, 30, 40, 50] 55 5 Larger than all elements, inserted at end
[5, 15, 25, 35] 25 2 Exact match at index 2
[5] 2 0 Inserted before the only element
[5] 5 0 Exact match at index 0
[5] 8 1 Inserted after the only element

Solution

Understanding the Problem

Suppose you are given a sorted array, and you want to find the correct position to insert a target number x so that the array remains sorted. This problem is not just about checking if x is present; it's about finding the position where it could be inserted — this is called the lower bound.

For example, if arr = [1, 3, 5, 6] and x = 5, the output should be 2 because 5 is already at index 2.

But if x = 4, then the correct insert position is 2 because it would go between 3 and 5.

Step-by-Step Intuition

Let’s solve this using a technique called binary search, which is perfect for sorted arrays. Instead of checking every position one by one, we’ll divide the array in halves, zero in on the region where x could go, and find the first index where arr[i] ≥ x.

Step 1: Initialize Search Range

We start with two pointers:

  • low = 0 (start of the array)
  • high = n - 1 (end of the array)

We also define ans = n (default answer is the end of array, in case x is greater than all elements).

Step 2: Binary Search Loop

Repeat while low ≤ high:

  • Calculate mid = Math.floor((low + high) / 2)
  • If arr[mid] ≥ x:
    • This index might be the correct insert position, so set ans = mid
    • Move search to the left half: high = mid - 1
  • Else:
    • x is greater than arr[mid], so move right: low = mid + 1

Step 3: Final Answer

After the loop ends, ans will hold the first index where x can be inserted to maintain the sorted order.

Example Walkthrough

Let’s try with arr = [1, 3, 5, 6] and x = 4.

  • low = 0, high = 3
  • mid = 1arr[1] = 3 → 3 < 4 → move right → low = 2
  • mid = 2arr[2] = 5 → 5 ≥ 4 → ans = 2, high = 1
  • Loop ends → Final answer: 2

Handling Edge Cases

  • x is smaller than all elements → e.g., x = 0 → insert at index 0
  • x is greater than all elements → e.g., x = 10 → insert at index arr.length
  • x is equal to an existing value → insert at the first occurrence (lower bound)

Why Binary Search?

Binary search gives us an efficient way to solve this in O(log n) time instead of scanning every element. This is especially useful for large arrays.

Algorithm Steps

  1. Given a sorted array arr of distinct integers and a target value x.
  2. Initialize: low = 0, high = arr.length - 1, ans = arr.length.
  3. While low ≤ high:
  4. → Compute mid = Math.floor((low + high) / 2).
  5. → If arr[mid] ≥ x, update ans = mid and move left: high = mid - 1.
  6. → Else, move right: low = mid + 1.
  7. Return ans as the correct insert position.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#include <stdio.h>

int searchInsertPosition(int arr[], int size, int target) {
  int low = 0, high = size - 1, ans = size;
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (arr[mid] >= target) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  return ans;
}

int main() {
  int arr[] = {1, 3, 5, 6};
  int size = sizeof(arr) / sizeof(arr[0]);
  int target = 2;
  printf("Insert Position: %d\n", searchInsertPosition(arr, size, target));
  return 0;
}

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(1)The target is found at the middle index on the first comparison, requiring only one iteration.
Average CaseO(log n)Binary search halves the search space in each iteration, so on average, it takes log n steps to find the insert position.
Worst CaseO(log n)In the worst case, the entire array must be searched logarithmically to determine where the target should be inserted.

Space Complexity

O(1)

Explanation: The algorithm operates in-place using only a constant amount of memory — just a few integer variables for low, high, mid, and ans.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...