279. First and last occurrence

Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If the target is not found in the array, return [-1, -1].

Example 1:

Input: nums = [5, 7, 7, 8, 8, 10], target = 8

Output: [3, 4]

Explanation:The target is 8, and it appears in the array at indices 3 and 4, so the output is [3,4]

Example 2:

Input: nums = [5, 7, 7, 8, 8, 10], target = 6

Output: [-1, -1]

Expalantion: The target is 6, which is not present in the array. Therefore, the output is [-1, -1].

Now Your Turn!

Pick the correct output for the given input

Input: nums = [5, 7, 7, 8, 8, 10], target = 5

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  •   0 <= nums.length <= 105
  •   -109 <= nums[i] <= 109
  •   nums is a non-decreasing array.
  •   -109 <= target <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution{
public:
vector<int> searchRange(vector<int> &nums, int target) {
}
};
Test Case

Input:

Nums
Target