Thursday, August 10th
Value in a pivoted array, part II
Given a pivoted array originally sorted in non-decreasing order, return whether or not a given target value exists in the array.
This is similar to Tuesday's problem, except that the array may contain duplicate values
In coming up with an algorithmic solution, it can be helpful to come up with worst-case or adversarial input. After all, Big-O notation is about finding an upper bound on the problem.
So consider the following array as input to the problem:
[4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 4, 4]
Where one is looking for a target value of 5 (or even a number not in the array, like 6)
There is no way binary search can work on an input like this. Binary search works by reducing the problem space by half each iteration. With all the 4's, there is no way binary search would know which half of the array to cut off.
So if the given input array can't be solved in logarithmic time, how about linear time.
A simple scan through the array can easily provide a solution
This answer is accepted by the LeetCode. It performs faster, and with less memory, than 90% of accepted solutions. If a linear solution isn't fast enough, it shouldn't work in all cases, but as I showed in my example above linear is acceptable in worst case.
Moreover, the length of the array is only a max of 5,000 numbers, a relatively short size. There would unlikely be a logarithmic solution that would work while a linear solution would not.
The posted solutions, both by the Editorial and by other users, implement a modified binary search that accommodates for duplicates. In the problem description, it says "You must decrease the overall operation steps as much as possible," but that is ambiguous. Again in my example above with the array of 4's and 5, how do you reduce the number of operations?
Some may point out that in average case, or when all or most values are unique, that the binary search method may perform better. Perhaps the problem should have been more specific and said log(n) in average case or no linear scans. In an interview I could see questioners ruling out a linear scan, and in a production environment you may want the fastest possible. Or the problem here is just to learn facets of a technique like binary search.
But the worst-case solution for this problem is misleading, and there's something to be said about simple solutions for complex problems, especially when it meets all the requirements for the problem at hand.
Comments
Post a Comment