Find First and Last Position of Element in Sorted Array - Problem
Search Range in Sorted Array

You are given a sorted array of integers and need to find the starting and ending positions of a specific target value. This is a classic problem that tests your understanding of binary search optimization.

The Challenge: Given an array nums sorted in non-decreasing order, find the first and last occurrence of a target value. If the target doesn't exist, return [-1, -1].

The Twist: You must solve this in O(log n) time complexity, which means a simple linear scan won't cut it!

Example:
Input: nums = [5,7,7,8,8,8,10], target = 8
Output: [3,5] (target 8 appears from index 3 to 5)

This problem is fundamental for understanding binary search variations and is frequently asked in technical interviews at major tech companies.

Input & Output

example_1.py โ€” Basic Range Search
$ Input: nums = [5,7,7,8,8,8,10], target = 8
โ€บ Output: [3,5]
๐Ÿ’ก Note: Target 8 appears at indices 3, 4, and 5. The first occurrence is at index 3 and the last occurrence is at index 5, so we return [3,5].
example_2.py โ€” Target Not Found
$ Input: nums = [5,7,7,8,8,8,10], target = 6
โ€บ Output: [-1,-1]
๐Ÿ’ก Note: Target 6 does not exist in the array, so we return [-1,-1] as specified in the problem statement.
example_3.py โ€” Empty Array
$ Input: nums = [], target = 0
โ€บ Output: [-1,-1]
๐Ÿ’ก Note: The array is empty, so the target cannot be found. We return [-1,-1].

Constraints

  • 0 โ‰ค nums.length โ‰ค 105
  • -109 โ‰ค nums[i] โ‰ค 109
  • nums is a non-decreasing array
  • -109 โ‰ค target โ‰ค 109
  • Follow up: Could you write an algorithm with O(log n) runtime complexity?

Visualization

Tap to expand
Binary Search Range Detection StrategySearch for First OccurrenceWhen target found: right = mid - 1Continue searching LEFTFind leftmost positionResult: index 3Search for Last OccurrenceWhen target found: left = mid + 1Continue searching RIGHTFind rightmost positionResult: index 5Array: [5, 7, 7, 8, 8, 8, 10]Final Answer: [3, 5] - Target 8 spans from index 3 to 5Time Complexity: O(log n) | Space Complexity: O(1)
Understanding the Visualization
1
Initial Setup
Set up two binary searches with left and right pointers
2
Find First Occurrence
When target found, continue searching left to find the earliest occurrence
3
Find Last Occurrence
When target found, continue searching right to find the latest occurrence
4
Return Range
Combine results to return [first, last] positions
Key Takeaway
๐ŸŽฏ Key Insight: Use two modified binary searches - one biased towards finding the leftmost occurrence, another biased towards finding the rightmost occurrence. This achieves O(log n) complexity by leveraging the sorted array property.
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 24 Apple 19
73.9K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen