 All Problems
Find First and Last Position in Sorted Array
medium
binary search
array
amazon
google
microsoft
facebook

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

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: 5 7 7 8 8 10
Target: 8
Output: 3 4

Example 2:

Input: 5 7 7 8 8 10
Target: 6
Output: -1 -1

Input format: First line: sorted space-separated array. Second line: target.

Output format: Two space-separated integers — start and end index.

Run to check your code against the sample cases, or submit to run every case