Time Complexity of Binary Search
Determine the time complexity (Big-O notation) of the following algorithm.
public int binarySearch(int[] sorted, int target) {
int low = 0, high = sorted.length - 1;
while(low <= high) {
int mid = (low + high) / 2;
if(sorted[mid] == target) return mid;
else if(sorted[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
A
O(log n)
B
O(n)
C
O(1)
D
O(n log n)
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | bommasam000 | 2 | 5 | 1m 39s | 71 |
| #2 | suhanakochhar006 | 1 | 2 | 1m 05s | 25 |
| #3 | geethasailaja | 1 | 2 | 1m 41s | -11 |
| #4 | y.seong2027 | 1 | 1 | 2m 46s | -66 |
| #5 | singhris000 | 1 | 2 | 7m 40s | -370 |
APFIVE