instagram

Find first and last index of target element from sorted array

Find first and last index of target element from sorted array

The best way we can do this with binary search. Big O will be O(2log(n)).

Input :

int a[] = { 1, 4, 5, 7, 8, 8, 8, 9, 10 };
Target : 8

Output :
Start :4 and End :6

Algorithm

Share