01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

Vocabulary you'll need.

Used in position-based search.

Vocabulary
  • 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.
04
AP Exam Focus

How argmax is tested.

Index vs value confusion is the dominant trap.

Exam Focus
  • 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 - 2 inclusive.
  • Initialize from arr[0]. Not 0 — that fails on all-negative arrays.
05
Worked Example

Find argmax with ties.

Strict comparison keeps the first occurrence.

Worked Example AP-style reasoning

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).

06
Common Mistakes

Argmax pitfalls.

Most flip index and value.

Watch Out
  • 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 - 1 crashes on neighbor access.
07
Reference Table

Argmax and peak detection.

Two related patterns side by side.

Argmax Reference

AP Quick Reference
TaskLoop rangeUpdate
Argmax1..length-1Both bestIndex and bestValue.
Argmin1..length-1Same pattern, <.
Peak1..length-2Compare both neighbors.
Trough1..length-2Compare both neighbors.
08
Practice Tip

How to master argmax.

Initialize from arr[0]; update together.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 07 Topic 03 Argmax and Adjacent-Element Comparison FRQ Practice

AP-style topic practice assessment

Start