Binary Search is an efficient algorithm for finding an element in a sorted array. It works by repeatedly dividing the search interval in half, eliminating half of the remaining elements at each step.
Array: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Target: 13
Step 1: Calculate mid = (0 + 9) / 2 = 4
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
↑
mid = 9
9 < 13, so search in the right half
Step 2: Calculate mid = (5 + 9) / 2 = 7
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
↑
mid = 15
15 > 13, so search in the left half
Step 3: Calculate mid = (5 + 6) / 2 = 5
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
↑
mid = 11
11 < 13, so search in the right half
Step 4: Calculate mid = (6 + 6) / 2 = 6
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
↑
mid = 13
13 == 13, element found at index 6!
- When the array is sorted
- When you need to find a specific element or the insertion position of an element
- When you need to find the first or last occurrence of an element
- When you need to find the closest element to a target
- When you need to search in a rotated sorted array
- When you need to find a peak element
- When you need to minimize the maximum or maximize the minimum value
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2 # Avoid integer overflow
if arr[mid] == target:
return mid # Element found
elif arr[mid] < target:
left = mid + 1 # Search in the right half
else:
right = mid - 1 # Search in the left half
return -1 # Element not found
# Example usage
arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
target = 13
print(binary_search(arr, target)) # Output: 6function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2); // Avoid integer overflow
if (arr[mid] === target) {
return mid; // Element found
} else if (arr[mid] < target) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}
return -1; // Element not found
}
// Example usage
const arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
const target = 13;
console.log(binarySearch(arr, target)); // Output: 6Python:
def find_first_occurrence(arr, target):
left, right = 0, len(arr) - 1
result = -1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
result = mid # Save the result
right = mid - 1 # Continue searching in the left half
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return result
# Example usage
arr = [1, 3, 5, 5, 5, 7, 9, 11]
target = 5
print(find_first_occurrence(arr, target)) # Output: 2JavaScript:
function findFirstOccurrence(arr, target) {
let left = 0;
let right = arr.length - 1;
let result = -1;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] === target) {
result = mid; // Save the result
right = mid - 1; // Continue searching in the left half
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
// Example usage
const arr = [1, 3, 5, 5, 5, 7, 9, 11];
const target = 5;
console.log(findFirstOccurrence(arr, target)); // Output: 2Python:
def find_last_occurrence(arr, target):
left, right = 0, len(arr) - 1
result = -1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
result = mid # Save the result
left = mid + 1 # Continue searching in the right half
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return result
# Example usage
arr = [1, 3, 5, 5, 5, 7, 9, 11]
target = 5
print(find_last_occurrence(arr, target)) # Output: 4JavaScript:
function findLastOccurrence(arr, target) {
let left = 0;
let right = arr.length - 1;
let result = -1;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] === target) {
result = mid; // Save the result
left = mid + 1; // Continue searching in the right half
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
// Example usage
const arr = [1, 3, 5, 5, 5, 7, 9, 11];
const target = 5;
console.log(findLastOccurrence(arr, target)); // Output: 4Python:
def find_insertion_position(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid # Element found
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return left # Insertion position
# Example usage
arr = [1, 3, 5, 7, 9]
target = 6
print(find_insertion_position(arr, target)) # Output: 3 (insert between 5 and 7)JavaScript:
function findInsertionPosition(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] === target) {
return mid; // Element found
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left; // Insertion position
}
// Example usage
const arr = [1, 3, 5, 7, 9];
const target = 6;
console.log(findInsertionPosition(arr, target)); // Output: 3 (insert between 5 and 7)Python:
def search_rotated_array(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid # Element found
# Check if the left half is sorted
if arr[left] <= arr[mid]:
# Check if target is in the left half
if arr[left] <= target < arr[mid]:
right = mid - 1
else:
left = mid + 1
# Right half is sorted
else:
# Check if target is in the right half
if arr[mid] < target <= arr[right]:
left = mid + 1
else:
right = mid - 1
return -1 # Element not found
# Example usage
arr = [4, 5, 6, 7, 0, 1, 2]
target = 0
print(search_rotated_array(arr, target)) # Output: 4JavaScript:
function searchRotatedArray(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] === target) {
return mid; // Element found
}
// Check if the left half is sorted
if (arr[left] <= arr[mid]) {
// Check if target is in the left half
if (arr[left] <= target && target < arr[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
// Right half is sorted
else {
// Check if target is in the right half
if (arr[mid] < target && target <= arr[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1; // Element not found
}
// Example usage
const arr = [4, 5, 6, 7, 0, 1, 2];
const target = 0;
console.log(searchRotatedArray(arr, target)); // Output: 4Python:
def find_peak_element(arr):
left, right = 0, len(arr) - 1
while left < right:
mid = left + (right - left) // 2
if arr[mid] > arr[mid + 1]:
# Peak is in the left half (including mid)
right = mid
else:
# Peak is in the right half
left = mid + 1
return left # Peak element index
# Example usage
arr = [1, 3, 4, 3, 5, 6, 4]
print(find_peak_element(arr)) # Output: 5 (value 6)JavaScript:
function findPeakElement(arr) {
let left = 0;
let right = arr.length - 1;
while (left < right) {
const mid = left + Math.floor((right - left) / 2);
if (arr[mid] > arr[mid + 1]) {
// Peak is in the left half (including mid)
right = mid;
} else {
// Peak is in the right half
left = mid + 1;
}
}
return left; // Peak element index
}
// Example usage
const arr = [1, 3, 4, 3, 5, 6, 4];
console.log(findPeakElement(arr)); // Output: 5 (value 6)| Algorithm | Time Complexity | Space Complexity |
|---|---|---|
| Binary Search | O(log n) | O(1) |
- Binary Search (Easy): Find a target value in a sorted array.
- Search in Rotated Sorted Array (Medium): Search for a target value in a rotated sorted array.
- Find First and Last Position of Element in Sorted Array (Medium): Find the starting and ending position of a given target value.
- Search Insert Position (Easy): Find the index where a target would be inserted in a sorted array.
- Find Peak Element (Medium): Find a peak element in an array.
- Find Minimum in Rotated Sorted Array (Medium): Find the minimum element in a rotated sorted array.
- Median of Two Sorted Arrays (Hard): Find the median of two sorted arrays.
- Koko Eating Bananas (Medium): Find the minimum eating speed to eat all bananas within a given time.
-
Use
left + (right - left) // 2instead of(left + right) // 2to avoid integer overflow. -
Be careful with the termination condition: Use
left <= rightfor standard binary search, andleft < rightfor some variations. -
Handle edge cases: Empty arrays, single-element arrays, and arrays with duplicate elements.
-
Be mindful of the search space: Make sure your search space includes the potential answer.
-
Check the boundary conditions: Ensure that your algorithm handles the first and last elements correctly.
-
Use binary search on the answer space: Sometimes, you can apply binary search on the answer space rather than the array itself.
-
Consider the problem constraints: Binary search is particularly useful when the time complexity requirement is O(log n).
-
Off-by-one errors: Be careful with the indices, especially when updating
leftandright. -
Infinite loops: Ensure that the search space is reduced in each iteration.
-
Not handling duplicates correctly: When there are duplicates, you might need to modify the standard binary search.
-
Incorrect mid calculation: Using
(left + right) / 2can lead to integer overflow for large arrays. -
Wrong termination condition: Using
left < rightinstead ofleft <= right(or vice versa) can lead to incorrect results.
Look for these clues in the problem statement:
- The input is sorted or partially sorted.
- You need to find a specific element or the insertion position of an element.
- The problem asks for an O(log n) solution.
- The problem involves finding the first/last occurrence, peak element, or minimum/maximum value.
- The problem involves minimizing the maximum or maximizing the minimum value.
- Keywords like "sorted," "search," "find," "minimum," "maximum," or "efficient search."
Here's a general template for binary search problems:
def binary_search(arr, target):
left, right = 0, len(arr) - 1 # Define the search space
while left <= right: # Continue until the search space is empty
mid = left + (right - left) // 2 # Calculate the middle index
if condition(mid): # Check if the middle element satisfies the condition
# Process the result (e.g., save it, return it)
# Adjust the search space (e.g., search in the left or right half)
else:
# Adjust the search space (e.g., search in the left or right half)
# Return the result or a default value- Database Indexing: Binary search is used in database indexes to quickly locate records.
- Compression Algorithms: Used in various compression algorithms to efficiently search for patterns.
- Machine Learning: Used in algorithms like binary decision trees.
- Computer Graphics: Used in ray tracing and collision detection algorithms.
- Network Routing: Used in routing algorithms to find the shortest path.
- Game Development: Used in pathfinding algorithms and AI decision-making.