...
/DIY: Find First and Last Position of an Element in Sorted Array
DIY: Find First and Last Position of an Element in Sorted Array
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]
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy