algorithm - Given a sorted array which is rotated 'N' times. Find the value of 'N'. The array can have duplicate elements -
for example if input (1,2,3,4,1) ans 4 (1,2,3,4)->0 (5,1,2,3)->1 if array not contain duplicate elements,it simple solve (using modified binary search) here,it contains duplicate elements ? in advance !!
i think original o(log n) approach no longer work in case array contains duplicate elements.
since there duplicate elements, cannot rule out half of array elements when searching "turning point" (the point array element max element, followed min element)
for example: {1, 1, 1, 1, 2, 1}, a[0] = a[5] = a[(0+5)/2] = 1. cannot discard half of elements there's not enough information indicating turning point lies.
so, pretty straight forward way o(n) sequential search turning point.
Comments
Post a Comment