Find Peak Element Using Binary Search in DSA Java - Time & Space Complexity
We want to understand how fast the binary search method finds a peak element in an array.
How does the number of steps change as the array gets bigger?
Analyze the time complexity of the following code snippet.
public int findPeakElement(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
This code uses binary search to find a peak element where the element is greater than its neighbors.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that halves the search space each time.
- How many times: The loop runs until the search space is reduced to one element, about log base 2 of n times.
Each step cuts the array size roughly in half, so the number of steps grows slowly as the array grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the input size adds only one extra step.
Time Complexity: O(log n)
This means the steps needed grow slowly, making the search very efficient even for large arrays.
[X] Wrong: "The loop runs n times because it looks at each element."
[OK] Correct: The loop does not check every element; it cuts the search area in half each time, so it runs much fewer times.
Understanding this time complexity shows you can use efficient searching methods, a key skill in many coding challenges and real projects.
"What if we changed the array to be sorted strictly increasing? How would the time complexity change?"
Practice
Solution
Step 1: Understand the definition of a peak element
A peak element is one that is strictly greater than its neighbors on both sides (or just one side if at the boundary).Step 2: Relate to binary search usage
Binary search finds such an element by comparing mid elements with neighbors to decide which half to search next.Final Answer:
An element that is greater than its neighbors -> Option BQuick Check:
Peak element = greater than neighbors [OK]
- Confusing peak with minimum element
- Thinking peak must be at array ends only
- Assuming peak equals neighbors
Solution
Step 1: Identify valid index range for binary search
Array indices in Java go from 0 to length-1, so left should be 0 and right should be arr.length - 1.Step 2: Check options for correct initialization
int left = 0, right = arr.length - 1; correctly sets left=0 and right=arr.length-1, which covers the whole array.Final Answer:
int left = 0, right = arr.length - 1; -> Option CQuick Check:
Index range = 0 to length-1 [OK]
- Starting right at arr.length causes out-of-bounds
- Starting left at 1 skips first element
- Using invalid index ranges
[1, 3, 20, 4, 1, 0] using binary search peak finding?Solution
Step 1: Identify peak elements in the array
Array: [1, 3, 20, 4, 1, 0]. 20 at index 2 is greater than neighbors 3 and 4, so it's a peak.Step 2: Binary search will find index 2 as peak
Binary search compares mid elements and moves left or right until it finds 20 at index 2.Final Answer:
2 -> Option DQuick Check:
Peak at index 2 = 20 [OK]
- Choosing first or last element incorrectly
- Confusing peak with global max
- Ignoring neighbors comparison
int mid = left + (right - left) / 2;
if (arr[mid] < arr[mid + 1]) {
left = mid + 1;
} else {
right = mid - 1;
}
return left;Solution
Step 1: Check boundary conditions for arr[mid + 1]
If mid is at the last index, arr[mid + 1] access causes ArrayIndexOutOfBoundsException.Step 2: Identify missing boundary check
The code does not check if mid + 1 is within array bounds before accessing.Final Answer:
It may cause ArrayIndexOutOfBoundsException at arr[mid + 1] -> Option AQuick Check:
Accessing arr[mid+1] at end causes error [OK]
- Ignoring array boundary checks
- Assuming mid+1 always valid
- Confusing left and right updates
[10, 20, 15, 2, 23, 90, 67]. Using binary search to find a peak element, which index will the algorithm return?Solution
Step 1: Identify all peak elements in the array
Array: [10, 20, 15, 2, 23, 90, 67]. Peaks are 20 (index 1), 90 (index 5).Step 2: Understand binary search behavior
Binary search compares mid with neighbors and moves towards the side with higher neighbor. It will find 90 at index 5 as peak.Final Answer:
5 -> Option AQuick Check:
Peak found at index 5 = 90 [OK]
- Assuming only first peak is returned
- Confusing peak with global max
- Not tracing binary search steps carefully
Ready to prove it?
Pass the test and this pattern moves unknown
