Find First and Last Position of Element in Sorted Array

Difficulty: Easy, Asked in: Google, Amazon, Microsoft.

Key takeaway: An excellent problem to learn problem-solving using binary search. Here we apply binary search twice to improve the time complexity.

Let's understand the problem

Given an array of integers sorted in ascending order, write a program to find the first and last position of a given target value.

  • If the target is not found in the array, return [-1, -1].
  • We must design an algorithm with O(log n) time complexity.

Example 1

Input: A[] = [-1, 1, 2, 2, 2, 5, 6, 12, 12], target = 2.

Output: First occurrence = 2, Last occurrence = 4.

Example 2

Input: A[] = [21, 32, 51, 70, 71], target = 70.

Output: First occurrence = 3, Last occurrence = 3.

Discussed solution approaches

  • Brute force approach  using linear search
  • Efficient approach  by applying binary search twice

Brute force approach using linear search

Solution idea

One basic solution is to traverse the array linearly and track the index of the first and last occurrence of the target. This approach is inefficient because we did not take advantage of the sorted order property.

Solution pseudocode

firstLastOccurrence(A, n, target)
    firstLast = [-1, -1]
    // Scan the array to find the first and last positions
    for i from 0 to n - 1
        if A[i] == target
            if firstLast[0] == -1
                firstLast[0] = i

            firstLast[1] = i

    return firstLast

Solution analysis

We are running a single loop n time and doing O(1) operations at each iteration. Time complexity = n * O(1) = O(n). We are using constant extra space, so space complexity = O(1).

Efficient approach using binary search

Solution idea and steps

Now the critical questions are: Can we improve the time complexity further? Can we use the sorted order property to enhance the efficiency of searching? Let's think!

As we know, binary search will take O(logn) time to search the target value in the sorted array. But the given problem is different from the binary search problem because we need to search for two occurrences of the target element. So, how can we modify the standard binary search algorithm to solve it?

The idea is to use binary search twice. We use the first binary search to find the first occurrence of the target, and the second binary search to find the last occurrence.

Binary search for finding the first occurrence

Let's use the iterative binary search! We first initialize low = 0 and high = n - 1 to track the left and right ends. Now we run a loop till low ≤ high:

  • We calculate mid, i.e. mid = low + (high - low)/2.
  • Now we compare the value at mid with the target. The middle element will be the first occurrence in two situations: 1) When the first occurrence is present somewhere in the middle i.e. target == A[mid] and target > A[mid - 1] 2) when the first occurrence is present at the first position i.e. mid == 0 and A[mid] == target. In both situations, we return the mid as the first occurrence.
  • If (target > A[mid]), we search for the first occurrence in the right part because all values in the left part are less than the target. Update low = mid + 1.
  • If both conditions are not satisfied, we need to search for the first occurrence in the left part. We update high = mid - 1.
  • If we did not find the target value by the end of the loop, we return -1.
Pseudocode to find the first occurrence
findFirstOccurrence(A, n, target)
    low = 0
    high = n - 1
    // Search for the first position containing target
    while low <= high
        mid = low + (high - low) / 2

        if A[mid] == target and (mid == 0 or A[mid - 1] < target)
            return mid
        else if A[mid] < target
            low = mid + 1
        else
            high = mid - 1

    return -1

Binary search for finding the last occurrence

Similarly, to find the last occurrence, we also modify the binary search algorithm. For this, we first initialize low = 0 and high = n - 1 to track the left and right ends. Now we run a loop until low ≤ high:

  • We calculate mid i.e. mid = low + (high - low)/2.
  • Next, we compare the value at mid with the target. The mid element will be the last occurrence in two situations: 1) When the last occurrence is present somewhere in the middle i.e. target == A[mid] and target < A[mid + 1] 2) When the last occurrence is at the last position i.e. mid == n - 1 and A[mid] == target.
  • If (target < A[mid]), we search for the last occurrence in the left part because all values in the right part are greater than the target. Update high= mid - 1.
  • If both conditions are not satisfied, we search for the last occurrence in the right part (Think). Update low = mid + 1.
  • By the end of the loop, if we did not find the target value, return -1.
Pseudocode to find the last occurrence
findLastOccurrence(A, n, target)
    low = 0
    high = n - 1
    // Search for the last position containing target
    while low <= high
        mid = low + (high - low) / 2

        if A[mid] == target and (mid == n - 1 or A[mid + 1] > target)
            return mid
        else if A[mid] > target
            high = mid - 1
        else
            low = mid + 1

    return -1

Complete implementation pseudocode

findLastOccurrence(A, n, target)
    low = 0
    high = n - 1
    // Search for the last position containing target
    while low <= high
        mid = low + (high - low) / 2
        if A[mid] == target and (mid == n - 1 or A[mid + 1] > target)
            return mid
        else if A[mid] > target
            high = mid - 1
        else
            low = mid + 1

    return -1

findFirstOccurrence(A, n, target)
    low = 0
    high = n - 1
    // Search for the first position containing target
    while low <= high
        mid = low + (high - low) / 2
        if A[mid] == target and (mid == 0 or A[mid - 1] < target)
            return mid
        else if A[mid] < target
            low = mid + 1
        else
            high = mid - 1

    return -1


findFirstLastOccurrence(A, n, target)
    firstLast = [-1, -1]
    firstLast[0] = findFirstOccurrence(A, n, target)
    firstLast[1] = findLastOccurrence(A, n, target)
    return firstLast

Solution analysis

We are applying binary search twice. Time complexity = Time complexity to search first occurrence + Time complexity to search last occurrence = O(1) + O(logn) + O(logn) = O(log n). We are using the implementation of iterative binary search, which takes O(1) extra space. Space complexity = O(1)

Critical ideas to think!

  • How can we implement the above approach using a recursive binary search? What will be the space complexity?
  • How can we find the first and last occurrences of a target value in an unsorted array? What will be the various approaches and their time and space complexities?
  • How can we find the first and last occurrence of a target in a sorted and rotated array?
  • How can we find the kth occurrence of the target? Suppose the target is appearing m times and m > k.

Comparisons of time and space complexities

  • Using linear search: Time = O(n), Space = O(1).
  • Using binary search: Time = O(logn), Space = O(1).

Coding problems to practice using binary search

Thanks to Navtosh for his contribution in creating the first version of this content. If you have any queries or feedback, please write us at contact@enjoyalgorithms.com. Enjoy learning, Enjoy algorithms!

More from EnjoyAlgorithms

Self-paced Courses and Blogs