34-find-first-and-last-position-in-sorted.cpp , several test cases may fail
Closed this issue · 1 comments
@Errichto
34-find-first-and-last-position-in-sorted.cpp (https://github.com/Errichto/youtube/tree/master/leetcode)
For this problem where you have defined the solution of finding the last position of the target in an array may fail due to the fact that we are subtracting one from the index and then checking it if that is less than or equal to the first position and will return -1,-1 which is incorrect; for example:-
array:- [5,1,7,8,8,10]
target - 10
the first_pos will be returned as 5 and last_pos as well be returned as 5 but we will subtract one and that will give us 4 and so 5 <= 4 we will have to return [-1,-1] which is incorrect instead of [5,5]
i think it shall be better to keep a separate call for the last position to avoid such scenarios.
Everything is fine. last_pos is returned as 6 and after subtraction it's interval [5,5]