Find First and Last Position of Element in Sorted Array | Leetcode problem number 34

preview_player
Показать описание
Find First and Last position of element in sorted array
Leetcode problem number 34

JAVA interview programming playlist:

#java #interviewquestions #leetcode
Рекомендации по теме
Комментарии
Автор

the question is asking for (logn) time complexity

TEJASSADADE
Автор

Failed a test case...
nums = [1], target = 1
Output= [0, -1]
Expected= [0, 0]
What should i do??

SpeedXArt
Автор

Wrong answer this code will break for 5, 7, 7, 8, 8, 8, 10 with char 8

raghuhg
Автор

nums=[1], target=1 it is giving "index 1 out of bounds for length 1". could you please help me.

tusharamale
Автор

Make Correction in the for loop as below as it will return an error of ArrayIndexOutOfBound:
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
arr[0] = i;
arr[1] = i;
while (i + 1 < nums.length && nums[i + 1] == target) {
i++;
arr[1] = i;
}
}
}

DevbrathBhattPujari
Автор

Hi there....I used my solution and your solution....nearly the same but it fails on the following test case

nums = [1, 3]
target = 1
Output = [0, -1]
Expected = [0, 0]

Can you please take a look, I feel test case is wrong somehow because it doesn't make sense to outout [0, 0] ideally it should be [0, -1] as per the problem statement.

ashishp-ci
Автор

not working for nums[ ]=1 && target =1.

RohanSingh-mynl
Автор

What if there are more than 2 occurrences of the target value?? This approach will fail!!

FaizanKhan-gfaizank