# Binary Search — Fast Lookup in Sorted Arrays (C++)
## Summary
Binary search locates a target in a sorted array by halving the search interval each step. Complexity is **O(log n)**.
## Requirements
- Input array **must be sorted**.
- Careful with bounds to avoid infinite loops and off-by-one errors.
## Implementation notes (repo)
The repository contains a simple binary search implementation `binary.cpp`. It signals success with a message when found; otherwise indicates not found.
### Typical iterative pattern
```cpp\nint l = 0, r = n-1; while (l <= r) { int mid = l + (r-l)/2; if (a[mid]== target) return mid; else if (a[mid]< target) l = mid + 1; else r = mid - 1; } return -1;\n```
## Pitfalls
- Ensure `l <= r` as loop condition for correctness.
- Use `l + (r-l)/2` to avoid overflow.
## Source
`https: //github.com/mashrur-rahman-fahim/algorithm/blob/main/binary.cpp`.