Java ProgramsArraysFind Peak Element in an Array

Find Peak Element in an Array in Java

intermediate·  Arrays  ·  Array

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.

Input
[2, 7, 15, 9, 3]
Output
Peak element: 15

Java Program

Java
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

Peak element: 15

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.

How It Works
  1. 1leftOk is true automatically at index 0 (no left neighbor to beat), otherwise it checks arr[i] &gt; arr[i - 1].
  2. 2rightOk is true automatically at the last index (no right neighbor to beat), otherwise it checks arr[i] &gt; arr[i + 1].
  3. 3A position that satisfies both conditions is a peak, and the loop breaks as soon as one is found.
  4. 4The array can have multiple valid peaks — this returns whichever one the left-to-right scan reaches first.
For [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.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

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

neighbor comparisonboundary conditionsearly exit with break

Approach 2: Binary Search

Java
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

Peak element: 15

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.

How It Works
  1. 1mid is computed as the midpoint between left and right.
  2. 2If arr[mid] &lt; arr[mid + 1], the sequence is still rising at mid, so a peak must exist somewhere to the right — left moves past mid.
  3. 3Otherwise, the sequence is falling (or mid is itself a peak), so a peak exists at mid or earlier — right shrinks down to mid.
  4. 4The loop ends once left and right converge, and that shared index is guaranteed to be a peak.
For [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.

Complexity
Time Complexity: O(log n)Space Complexity: O(1)

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.

Key Concepts

binary searchslope comparisonlogarithmic search

Related Programs