Find First And Last Position Of Element In Sorted Array

To solve this coding challenge, we need to find the first and last positions of a specified target value within a given array of integers that is sorted in non-decreasing order. If the target is not present in the array, we should return [-1, -1]. The problem requires that we solve it using an algorithm with O(log n) runtime complexity, which naturally suggests using binary search. The binary search algorithm is efficient for divide-and-conquer problems and performs search operations in logarithmic time complexity.

Explanation

We will approach the problem by applying binary search twice:
  1. To find the leftmost (first) position of the target.
  2. To find the rightmost (last) position of the target.
  3. The idea is to perform a binary search that specifically looks for the first occurrence and another binary search that finds the last occurrence of the target in the array. During the left binary search, when we find the target, we continue searching to the left. Conversely, during the right binary search, when we find the target, we continue searching to the right.
    Detailed Steps in Pseudocode
  4. Define a function called
    searchRange
    that takes
    nums
    and
    target
    as parameters
    .
  5. Create two helper functions:
    binary_search_left
    and
    binary_search_right
    .
    • *
      binary_search_left
      will find the leftmost index where the target can be found.
      *
      binary_search_right
      will find the rightmost index where the target can be found.
  6. Implement the
    binary_search_left
    function
    :
    • * Initialize
      left
      to 0 and
      right
      to the length of
      nums
      - 1.
      * While
      left
      is less than or equal to
      right
      , calculate
      mid
      .
      * If
      nums[mid]
      is less than the target, adjust
      left
      to
      mid + 1
      .
      * Otherwise, adjust
      right
      to
      mid - 1
      .
      * Return
      left
      after the loop ends.
  7. Implement the
    binary_search_right
    function
    :
    • * Initialize
      left
      to 0 and
      right
      to the length of
      nums
      - 1.
      * While
      left
      is less than or equal to
      right
      , calculate
      mid
      .
      * If
      nums[mid]
      is less than or equal to the target, adjust
      left
      to
      mid + 1
      .
      * Otherwise, adjust
      right
      to
      mid - 1
      .
      * Return
      right
      after the loop ends.
  8. Call
    binary_search_left
    and
    binary_search_right
    to get the positions
    .
  9. Check if the target is present :
    • * If
      left_idx
      is less than or equal to
      right_idx
      and
      nums[left_idx]
      equals
      target
      and
      nums[right_idx]
      equals
      target
      , return
      [left_idx, right_idx]
      .
      * Otherwise, return
      [-1, -1]
      .
Here's the pseudocode to solve the challenge:
                                            
# Main function to find the first and last position of the target in the array
function searchRange(nums, target):
    # Binary search to find the leftmost position of the target
    function binary_search_left(nums, target):
        left = 0
        right = length(nums) - 1
        while left <= right:
            mid = left + (right - left) // 2
            if nums[mid] < target:
                left = mid + 1
            else:
                right = mid - 1
        return left  # This is the leftmost index where target would be

    # Binary search to find the rightmost position of the target
    function binary_search_right(nums, target):
        left = 0
        right = length(nums) - 1
        while left <= right:
            mid = left + (right - left) // 2
            if nums[mid] <= target:
                left = mid + 1
            else:
                right = mid - 1
        return right  # This is the rightmost index where target would be

    # Find the indices using the helper functions
    left_idx = binary_search_left(nums, target)
    right_idx = binary_search_right(nums, target)

    # Verify if the target is within the bounds and positions are correct
    if left_idx <= right_idx and left_idx < length(nums) and nums[left_idx] == target and right_idx < length(nums) and nums[right_idx] == target:
        return [left_idx, right_idx]  # Return the indices where the target starts and ends
    else:
        return [-1, -1]  # Target is not present in the array

                                        
This pseudocode provides a step-by-step implementation of the solution, ensuring the detailed use of binary search to maintain the O(log n) runtime complexity as required by the problem.