Searching/Binary Search
sorteddivide-and-conquerefficient
Press play to start
1int binarySearch(int arr[], int n, int target) {
2 int low = 0, high = n - 1;
3 while (low <= high) {
4 int mid = (low + high) / 2;
5 if (arr[mid] == target) return mid;
6 else if (arr[mid] < target) low = mid + 1;
7 else high = mid - 1;
8 }
9 return -1;
10}
Step 1/0

Practice

LeetCode·#704 Binary SearchEasyHackerRank·Binary Search (FP)MediumNeetCode·Binary SearchEasy
BestO(1)
AverageO(log n)
WorstO(log n)
SpaceO(1)