Find First and Last Position of Element in Sorted Array
Given an array of integers nums and an integer target, return the indices of the first and last occurrence of target in nums. If target is not found in nums, return [-1, -1].
Example 1:
Input:nums = [5,7,7,8,8,10], target = 8
Output:[3,4]
Example 2:
Input:nums = [5,7,7,8,8,10], target = 6
Output:[-1,-1]
Example 3:
Input:nums = [5,7,7,8,8,10], target = 5
Output:[0,0]
Constraints:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums is a non-empty array containing only distinct values
-109 <= target <= 109
solution.cpp
Loading...
Run code to see test results
00:00 / 69:42
Find First and Last Position of Element in Sorted Array
MediumMed•610 views
Given an array of integers nums and an integer target, return the indices of the first and last occurrence of target in nums. If target is not found in nums, return [-1, -1].
Example 1:
Input:nums = [5,7,7,8,8,10], target = 8
Output:[3,4]
Example 2:
Input:nums = [5,7,7,8,8,10], target = 6
Output:[-1,-1]
Example 3:
Input:nums = [5,7,7,8,8,10], target = 5
Output:[0,0]
Constraints:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums is a non-empty array containing only distinct values