The optimal approach is to use binary search. First, use binary search to find the first occurrence of the target element, and then use it again to find the last occurrence. This approach has a time complexity of O(log n), which is much faster than a linear scan for large arrays.
Find First and Last Position of Element in Sorted Array LeetCode
In many real-world scenarios, efficiently finding the starting and ending positions of a target value in a sorted array is essential. For instance, in systems monitoring, log files record events with timestamps, etc. To investigate issues, it's often necessary to locate the range of log entries for a specific event type. Similar problems arise in other contexts as well, such as searching library databases for books published in a specific year, analyzing customer transaction records for a particular date, identifying specific sensor readings in IoT systems, and querying movie databases for films released in a given year. In all these cases, quick and accurate access to specific data ranges is important.
Key takeaways
The problem involves finding the starting and ending positions of a target value in a sorted array.
The problem can be solved using binary search to achieve O(log n) time complexity.
By slightly modifying binary search, we can locate both the first and last occurrences of the target.
The algorithm has a space complexity of O(1) due to minimal additional space usage.
The approach is ideal for handling large datasets with quick access to data ranges.
Problem statement
Given an array of integers nums sorted in non-decreasing order and an integer target, find the starting and ending position of the given target value within the array.
If target is not found in the array, return [-1, -1].
Constraints
0 <= nums.length <= 105-109<= nums[i] <= 109numsis a non-decreasing array.-109<= target <= 109
Note: Our algorithm's runtime complexity is
.
Example
Knowledge test
Now that we have understood the problem, let’s check our knowledge by solving the MCQs given below:
Choose the correct option:
Given the sorted array nums = [1, 2, 2, 2, 3, 4, 5] and target = 2, what will be the output of the function that finds the starting and ending position of the target value?
[1, 3]
[2, 4]
[1, 4]
[0, 2]
Solution using binary search
To write an algorithm with
Algorithm steps
Define the
findBoundfunction:Takes three arguments: the array
nums, the target valuetarget, and a booleanisFirstto indicate whether to find the first or last occurrence of the target.
Initialize variables:
Set
beginto 0.Set
endto the last index of the array.
Iterate until
beginis greater thanend:Calculate the middle index
midas(begin + end) / 2.Use the value of the middle element to decide the next steps.
Handle cases based on comparison with
target:If
nums[mid] == target:For first occurrence (
isFirstistrue):If
mid == beginornums[mid - 1] != target, returnmid.Otherwise, update
end = mid - 1.
For last occurrence (
isFirstisfalse):If
mid == endornums[mid + 1] != target, returnmid.Otherwise, update
begin = mid + 1.
If
nums[mid] > target:Update
end = mid - 1.
If
nums[mid] < target:Update
begin = mid + 1.
Return -1 If target not found:
At the end of the
findBoundfunction, return -1 if the target was not found.
Implement the
searchRangefunction:Call
findBoundwithisFirstset totrueto find the first occurrence.If the result is
, return [-1, -1]since the target is not in the array.Otherwise, call
findBoundwithisFirstset tofalseto find the last occurrence.Return the result as
[first_occurrence, last_occurrence].
This algorithm ensures that we efficiently locate the boundaries of the target value in a sorted array with a logarithmic runtime, making it suitable for large datasets.
Illustration
The following illustration shows the algorithm explained above:
Code
Let’s have a look at the code for the algorithm we just discussed.
def find_bound(nums, target, isFirst):# Initialize the beginning and end indicesbegin, end = 0, len(nums) - 1# Perform binary searchwhile begin <= end:# Calculate the middle indexmid = (begin + end) // 2# Check if the middle element is the targetif nums[mid] == target:if isFirst:# Check if it is the first occurrenceif mid == begin or nums[mid - 1] != target:return midelse:end = mid - 1else:# Check if it is the last occurrenceif mid == end or nums[mid + 1] != target:return midelse:begin = mid + 1elif nums[mid] > target:# Adjust end index for left subarrayend = mid - 1else:# Adjust begin index for right subarraybegin = mid + 1return -1def search_range(nums, target):# Find the first occurrence of targetfirst = find_bound(nums, target, True)# If the target is not found, return [-1, -1]if first == -1:return [-1, -1]# Find the last occurrence of targetlast = find_bound(nums, target, False)return [first, last]# Driver code with examplesexamples = [([5, 7, 7, 8, 8, 10], 8),([5, 7, 7, 8, 8, 10], 6),([2, 2, 2, 2, 2], 2),([1, 3, 5, 7], 7),([1, 3, 5, 7], 4)]for nums, target in examples:result = search_range(nums, target)print(f"Array: {nums}, Target: {target}, Result: {result}\n")
Time complexity
The time complexity of the provided algorithm is
Space complexity
The space complexity is
Resources for preparation
To effectively prepare for the coding interview, consider leveraging some of the most popular resources from Educative as follows:
You may consider the following course to enhance your C++ skills based on your experience:
Preparing for operating system and object oriented design and analysis, following are a well suited courses:
Good luck with your preparation!
Frequently asked questions
Haven’t found what you were looking for? Contact Us
What is the optimal approach to solve the "Find First and Last Position of Element in Sorted Array" problem?
How do I handle cases where the target element is not present in the array?
Why is binary search used instead of a linear search for this problem?
Free Resources