DIY: Find First and Last Position of an Element in Sorted Array
Solve the interview question "Find First and Last Position of an Element in Sorted Array" in this lesson.
We'll cover the following
Problem statement
In this challenge, you are given an integer array called numbers
as input. This array will be sorted in ascending order. You will also be given a target
integer value as input. Your task is to find the starting and ending positions of the given target
value in the array.
If the
target
is not found in thenumbers
array, your solution should return[-1, -1]
.
Input
The function will have two inputs: an integer array called numbers
and an integer value called target
. The following is an example of the inputs:
numbers = [2,4,5,5,6,6,6,7,7,8,10,10]
target = 6
Output
The function’s output will be an array containing two integers representing the indices of the first and last occurrences of the target
value in the array. The following is the output of the inputs above.
[4, 6]
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.