Binary Search | Easy | LeetCode#704 Binary Search
🌐 中文版:繁體中文
Problem statement

The problem gives an integer array nums that is sorted, and we want to find the index of the element target in it, returning -1 if it isn’t found. It also requires that the algorithm run in $O(\log n)$ time.
Solution
My approach
1 | int search(int* nums, int numsSize, int target) { |
This uses the standard binary search approach. First we set the middle value mid where we split, taking the average of low and high to find the midpoint. Then we start judging the cases:
- When
nums[mid] > target: the midpoint is greater than the target, meaning we can discard the right half ofmidand search only the left half, so we adjusthighto shrink the search rangehigh = mid -1;
- When
nums[mid] < target: the midpoint is less than the target, meaning we can discard the left half ofmidand search only the right half, so we adjustlowto shrink the search range low = mid +1;- The third case is finding the
targetvalue in the array, so we returnmid - The while loop’s termination condition is
low > high, meaning we’ve searched too many times and confirmed the array has no target value, so we end the loop andreturn -1
Note: for the midpoint it’s better to rewrite it as
mid = low + (high - low)/2. Doing so avoids the overflow problem when bothhighandloware large numbers and their sum overflows.
Execution result

Another approach: finding the upper bound

This is another approach I saw in the LeetCode solutions section. Rather than finding the Target itself directly, find the position where Target could be inserted into the array.
This solution first defines the block that can be inserted into, as the picture describes: for an array like [-7,-4,3,9,9,9,12], the largest position it could be inserted at is between 9 and 12, and the smallest position it could be inserted at is between 3 and 9.
First, same as before, we need to compute the midpoint position, then when judging we consider separately:
- When
nums[mid] < target: the midpoint is less than the target, so the possible insert position is to the right ofmid, so we adjust thelowrange and keep looking for the upper bound low = mid +1;- When
nums[mid] == target: the midpoint equals the target, so the possible insert position is to the right ofmid, so we adjust thelowrange and keep looking for the upper bound low = mid +1;- When
nums[mid] > target: the midpoint is greater than the target, so the possible insert position is to the left ofmid, so we adjust thehighrange and keep looking for the upper bound. Heremidmight also be an insertable position, so we sethightomidhigh = mid;
Once the loop ends, high represents the insert position, and low - 1 represents the largest element not greater than target. So after it ends we need to check whether nums[low-1] equals target; if so return low-1, otherwise return -1.
The algorithm is as follows:
1 | int search(int* nums, int numsSize, int target) { |
Why does this method find the target?
- Merging conditions: in this method we merge the two conditions
nums[mid] < targetandnums[mid] == target, because whether it’s less than or equal, the target’s insert position should be to the right ofmid. So in both cases we setlefttomid + 1. - Keeping the valid range: if
nums[mid] > target, the target should be atmidor to its left, so we setrighttomidrather thanmid - 1, keepingmidas a possible position. - Loop termination check: when
leftandrightare equal, the loop ends, andleftis the target’s insert position. If the target exists in the array, thenleft - 1is the last position of the target. At that point we just need to check whethernums[left - 1]equals the target.
Special cases
- All elements in the array are greater than the target: in this case
leftends up equal to 0, which means the target doesn’t exist in the array. - All elements in the array are less than the target: in this case
leftends up equal to the array length, and the target should be inserted at the last position of the array.
Execution result

Time complexity
The number of steps N -> 1/2 N -> 1/4 N -> 1/8 N … converges to a constant following the $\log n$ function.








