Find Peak Element in an Array in Java
Problem
A peak element is one that is strictly greater than the elements immediately next to it — an edge element only needs to beat its single neighbor.
Given an array of integers, find any one peak element it contains.
Java Program
public class PeakElement {
public static void main(String[] args) {
int[] arr = {2, 7, 15, 9, 3};
int peak = arr[0];
for (int i = 0; i < arr.length; i++) {
boolean leftOk = (i == 0) || arr[i] > arr[i - 1]; // no left neighbor to beat at index 0
boolean rightOk = (i == arr.length - 1) || arr[i] > arr[i + 1]; // no right neighbor at the last index
if (leftOk && rightOk) {
peak = arr[i];
break; // found a peak, no need to keep scanning
}
}
System.out.println("Peak element: " + peak);
}
}Output
Core Logic
Checking each position against both of its neighbors, treating the array's edges as having only one neighbor to satisfy, finds a peak in a single left-to-right scan.
- 1
leftOkistrueautomatically at index0(no left neighbor to beat), otherwise it checksarr[i] > arr[i - 1]. - 2
rightOkistrueautomatically at the last index (no right neighbor to beat), otherwise it checksarr[i] > arr[i + 1]. - 3A position that satisfies both conditions is a peak, and the loop
breaks as soon as one is found. - 4The array can have multiple valid peaks — this returns whichever one the left-to-right scan reaches first.
[2, 7, 15, 9, 3], index 2 (value 15) beats both neighbors — 7 on the left and 9 on the right — so it's reported as the peak.Key Point: 'A' peak, not 'the' peak — an array can have several elements that each beat both their neighbors, and any one of them is a correct answer.
Why: In the worst case every element is checked once before a peak is found, and only two boolean flags are kept per position.
Key Concepts
Approach 2: Binary Search
public class PeakElementBinarySearch {
public static void main(String[] args) {
int[] arr = {2, 7, 15, 9, 3};
int left = 0, right = arr.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (arr[mid] < arr[mid + 1]) {
left = mid + 1; // a peak exists to the right
} else {
right = mid; // a peak exists at mid or to the left
}
}
System.out.println("Peak element: " + arr[left]);
}
}
Output
Core Logic
Comparing the middle element to its right neighbor reveals which half of the array must contain a peak — a rising slope means one exists further right, a falling slope means one exists at or before the midpoint.
- 1
midis computed as the midpoint betweenleftandright. - 2If
arr[mid] < arr[mid + 1], the sequence is still rising atmid, so a peak must exist somewhere to the right —leftmoves pastmid. - 3Otherwise, the sequence is falling (or
midis itself a peak), so a peak exists atmidor earlier —rightshrinks down tomid. - 4The loop ends once
leftandrightconverge, and that shared index is guaranteed to be a peak.
[2, 7, 15, 9, 3], the search narrows from the full array down to index 2 in just two comparisons, landing on the same peak, 15.Key Point: This works even though the array isn't sorted — the algorithm only ever relies on the local slope between adjacent elements, not on any global ordering.
Why: The search range is cut roughly in half at every step, so the number of comparisons grows logarithmically instead of linearly with the array's length.