Introduction
The Binary Search algorithm is an efficient searching technique used on sorted arrays.
Given a sorted array arr[] and a target element x, the task is to find the index of the target element.
Binary Search works by:
- repeatedly dividing the search space into half
- comparing the middle element with target
- eliminating unnecessary portions of the array
This problem helps in understanding:
- divide and conquer
- efficient searching
- pointer manipulation
- logarithmic optimization
Example
Input: arr[] = [1,3,5,7,9,11]x = 7
Output: 3
Explanation:
Element 7 is present at index 3.
Input: arr[] = [2,4,6,8,10]
x = 5
Output: -1
Explanation:
Element 5 does not exist in the array.
Constraints
1 <= n <= 10^5-10^9 <= arr[i] <= 10^9-10^9 <= x <= 10^9Array is sorted
Approach 1 : Brute Force (Linear Search)
Explanation
The simplest way to solve this problem is:
- Traverse the array sequentially
- Compare every element with target
- Return index if found
This approach works but does not utilize the sorted property of the array.
Steps
- Traverse the array.
- Compare current element with target.
- If found:
- return index
- Otherwise:
- continue traversal
- Return -1 if target not found.
Dry Run
Input Array:[1,3,5,7,9,11]
Target = 7
Traverse 1:
1 != 7
Traverse 3:
3 != 7
Traverse 5:
5 != 7
Traverse 7:
7 == 7
Element found at index 3
Return 3
Brute Force Code
Complexity Analysis
Time Complexity: O(n)Explanation:
Every element may need to be checked.
Space Complexity: O(1)
Explanation:
No extra data structures are used.
Approach 2 : Optimized Solution (Binary Search)
Explanation
Since the array is sorted, we can use Binary Search.
The idea is:
- Find middle element
- Compare middle element with target
- If:
- middle == target:
- return index
- target < middle:
- search left half
- target > middle:
- search right half
- middle == target:
This repeatedly reduces the search space by half.
Steps
- Initialize:
- left = 0
- right = n - 1
- While left ≤ right:
- find middle index
- Compare middle element with target.
- If target smaller:
- move right pointer
- If target greater:
- move left pointer
- Return index if found.
- Otherwise return -1.
Dry Run
Input Array:[1,3,5,7,9,11]
Target = 7
Initially:
left = 0
right = 5
Middle = 2
arr[2] = 5
5 < 7
Move left pointer
left = 3
right = 5
Middle = 4
arr[4] = 9
9 > 7
Move right pointer
right = 3
Middle = 3
arr[3] = 7
Target found
Return 3
Optimized Code
Complexity Analysis
Time Complexity: O(log n)Explanation:
Search space is reduced by half in every iteration
Space Complexity: O(1)
Explanation:
No extra data structures are used.
Edge Cases
- Element present at beginning
- Element present at end
- Element not present
- Array contains one element
- Large sorted arrays
Why This Problem is Important
This problem helps in understanding:
- Divide and conquer
- Logarithmic optimization
- Efficient searching
- Pointer manipulation
- Sorted array processing
It is one of the most important searching interview problems.
Real-World Applications
Binary Search is used in:
- Database indexing
- Search engines
- Dictionary lookup
- Operating systems
- Large-scale data retrieval
Common Mistakes
- Incorrect middle calculation
- Infinite loop conditions
- Wrong pointer updates
- Forgetting sorted array requirement
Interview Tips
Interviewers often expect:
- O(log n) Binary Search
- Proper pointer updates
- Middle element explanation
Always explain how Binary Search reduces search space efficiently.
Related Questions
- Search Insert Position
- First Occurrence
- Last Occurrence
- Peak Element
- Rotated Sorted Array Search
Final Takeaway
Binary Search is one of the most important searching algorithms that teaches divide-and-conquer optimization and logarithmic searching techniques. Understanding this problem builds a strong foundation for advanced searching and optimization interview problems.