For this problem I used binary search, since the array is sorted. A better approach would be to use two pointers (left and right) moving inward. I recall seeing this problem before in “The Beauty of Programming” or “Erta to Offer”.
| |
For this problem I used binary search, since the array is sorted. A better approach would be to use two pointers (left and right) moving inward. I recall seeing this problem before in “The Beauty of Programming” or “Erta to Offer”.
| |