0% completed
For the similar problem, we never checked the 'end' value for the original solut...
Michael Shum
Feb 16, 2023
For the similar problem, we never checked the 'end' value for the original solution. so why should we check for that in the similar problem?
0
0
Comments
Design Gurus3 years ago
Are you talking about these lines?
if arr[end - 1] > arr[end]: # if the element at end is not the smallest return end
We need to check both start and end (not end only).
We didn't check both start and end in the original solution because there were no duplicates the...
Michael Shum3 years ago
Ok, I understood that after doing it another time.
Is there a reason when the start & mid & end have the same values, we cannot just 'start++; end--;' ? must we do the extra checks?
I could not find an example that fail when just doing start++, end-- .
Thank you! :)
Michael Shum3 years ago
If it's just to exit early, that's understandable. ty!
Design Gurus3 years ago
We need to return if we have found the required element. This should happen before start++ or end--.
We do need to exit at that point (it is not that we are exiting early, there is no use, if we keep searching).
On this page