https://youtu.be/f6UU7V3szVw?si=26WxPdb9UAUEqpAx
- Used for sorted arrays
- in ascending or descending order
- It works by repeatedly dividing the search interval in half and comparing the target with the middle element.
Algo
- find the middle element
- target ele > mid ele => search in right else search in left
- if mid ele == target ele => ans
Loops used in binary search
while if else if else
Time Complexity:
- Best Case: O(1) (target found at the middle).
- Worst Case: O(logn) (array size halved each step).
Space Complexity:
- O(1) (iterative approach, no extra memory used).
while coding