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:- To find the leftmost (first) position of the target.
- To find the rightmost (last) position of the target. 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.
-
Define a function called
searchRange
nums
target
-
Create two helper functions:
binary_search_left
binary_search_right
-
Implement the
binary_search_left
-
Implement the
binary_search_right
-
Call
binary_search_left
binary_search_right
- Check if the target is present :
Detailed Steps in Pseudocode
-
*
binary_search_left
binary_search_right
-
* Initialize
left
right
nums
left
right
mid
nums[mid]
left
mid + 1
right
mid - 1
left
-
* Initialize
left
right
nums
left
right
mid
nums[mid]
left
mid + 1
right
mid - 1
right
-
* If
left_idx
right_idx
nums[left_idx]
target
nums[right_idx]
target
[left_idx, right_idx]
[-1, -1]
# 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.