Finding the index of the maximum value.
When the position matters more than the value — argmax is the canonical answer.
Argmax is the index of the largest element, not the element itself. The pattern is "max plus position tracking": when a new maximum appears, both the value and its index update together.
Often combined with adjacent-element comparison — checking whether one element is larger than its neighbor — to locate peaks, find turning points, or detect specific positional patterns.
On the AP exam, argmax appears whenever a method needs to return a position rather than a value.
Track value and index together.
Update both whenever a strict improvement appears.
Argmax
public int argmax(int[] arr) {
int bestIndex = 0;
int bestValue = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > bestValue) {
bestValue = arr[i];
bestIndex = i;
}
}
return bestIndex;
}
Strict > keeps the earliest occurrence of the maximum. Use >= to keep the latest.
Adjacent comparison: peak detection
// A peak is greater than both neighbors
for (int i = 1; i < arr.length - 1; i++) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
// arr[i] is a peak
}
}
Loop starts at 1 and ends at length - 1 so both neighbors are in bounds.
Vocabulary you'll need.
Used in position-based search.
- Argmax — the index where the maximum lives.
- Argmin — the index where the minimum lives.
- Peak — an element greater than both neighbors.
- Trough — an element less than both neighbors.
- Tie-breaking — rule for when multiple positions share the extreme.
- Interior index — an index between 1 and
length - 2.
How argmax is tested.
Index vs value confusion is the dominant trap.
- Return the index. Returning the value misses the point.
- Strict vs non-strict comparison. Changes which tie wins.
- Interior loop for peaks. Bounds must be 1 to
length - 2inclusive. - Initialize from arr[0]. Not
0— that fails on all-negative arrays.
Find argmax with ties.
Strict comparison keeps the first occurrence.
Input: [3, 7, 2, 7, 5].
Trace. Start bestIndex=0, bestValue=3.
- i=1: 7 > 3 → bestIndex=1, bestValue=7.
- i=2: 2 > 7? No.
- i=3: 7 > 7? No (strict).
- i=4: 5 > 7? No.
Answer: 1 (first 7). If >= were used, the answer would be 3 (latest 7).
Argmax pitfalls.
Most flip index and value.
- Returning the value. When the prompt wants the index.
- Initializing bestValue to 0. Breaks for all-negative arrays.
- Updating only one of bestIndex and bestValue. They must move together.
- Wrong peak bounds. Including index 0 or
length - 1crashes on neighbor access.
Argmax and peak detection.
Two related patterns side by side.
Argmax Reference
AP Quick Reference| Task | Loop range | Update |
|---|---|---|
| Argmax | 1..length-1 | Both bestIndex and bestValue. |
| Argmin | 1..length-1 | Same pattern, <. |
| Peak | 1..length-2 | Compare both neighbors. |
| Trough | 1..length-2 | Compare both neighbors. |
How to master argmax.
Initialize from arr[0]; update together.
- MCQ Practice — distinguish strict vs non-strict before tracing ties.
- Java Lab — write argmax, argmin, findPeak. Test on monotone, all-equal, and all-negative arrays.
- FRQ Practice — return the index when the prompt asks for position; the value when it asks for value.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 03 Argmax and Adjacent-Element Comparison FRQ Practice
AP-style topic practice assessment