Problem Number: 704 Difficulty: Easy Category: Array, Binary Search LeetCode Link: https://leetcode.com/problems/binary-search/
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Constraints:
1 <= nums.length <= 10^4-10^4 < nums[i], target < 10^4- All the integers in
numsare unique. numsis sorted in ascending order.
I used a Recursive Binary Search approach to find the target in the sorted array. The key insight is to use divide-and-conquer strategy to reduce search space by half in each iteration.
Algorithm:
- Define recursive function with low and high bounds
- Base case: if low > high, return -1 (not found)
- Calculate mid point
- Check if target is at low, high, or mid
- If target < nums[mid], search left half
- If target > nums[mid], search right half
- Return index if found, -1 otherwise
The solution uses recursive binary search to efficiently find target in sorted array. See the implementation in the solution file.
Key Points:
- Uses recursive binary search
- Checks boundary conditions first
- Reduces search space by half each iteration
- Returns index or -1 if not found
Time Complexity: O(log n)
- Each recursive call reduces search space by half
- Total recursive calls: O(log n)
Space Complexity: O(log n)
- Recursive call stack depth: O(log n)
- Each call uses constant space
-
Divide and Conquer: Binary search reduces problem size by half each iteration.
-
Sorted Array: Binary search requires sorted array for O(log n) complexity.
-
Boundary Checks: Check low and high bounds before mid for efficiency.
-
Recursive Approach: Recursive implementation is clean and intuitive.
-
Early Termination: Can terminate early if target found at boundaries.
-
Unique Elements: Problem guarantees unique elements in array.
-
Wrong Bounds: Initially might use wrong bounds for recursive calls.
-
Infinite Recursion: Not handling base case properly.
-
Complex Logic: Overcomplicating the binary search logic.
-
Wrong Mid Calculation: Using wrong formula for mid calculation.
- Search Insert Position (Problem 35): Find insertion position
- Find First and Last Position (Problem 34): Find range of target
- Search in Rotated Sorted Array (Problem 33): Search in rotated array
- Find Minimum in Rotated Sorted Array (Problem 153): Find minimum element
- Iterative Binary Search: Use while loop instead of recursion - O(log n) time, O(1) space
- Built-in Binary Search: Use bisect module - O(log n) time
- Linear Search: Use linear search - O(n) time (not optimal)
- Wrong Bounds: Using wrong bounds for recursive calls.
- Infinite Recursion: Not handling base case properly.
- Complex Logic: Overcomplicating the binary search logic.
- Wrong Mid Calculation: Using wrong formula for mid calculation.
- Overflow: Not handling integer overflow in mid calculation.
Note: This is a binary search problem that demonstrates efficient divide-and-conquer strategy for searching in sorted arrays.