modified binary search of array for finding first and last match