Skip to main content
United StatesComputer ScienceSyllabus dot point

What are the standard array algorithms - finding a sum, maximum, minimum, count or average - and how do you write them?

Topic 6.4 Developing Algorithms Using Arrays: write and apply standard algorithms over a one-dimensional array, including finding minimum, maximum, sum, average, count and shifting or rearranging elements.

A focused answer to AP CSA Topic 6.4, covering the standard array algorithms - sum, average, minimum, maximum, count, search, and rearranging elements - the accumulator and running-best patterns, and how to combine them, with a fully worked example.

Generated by Claude Opus 4.812 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Sum and average
  3. Maximum and minimum
  4. Count and search
  5. Rearranging elements
  6. Try this

What this topic is asking

The College Board (Topic 6.4) wants you to write standard algorithms that process a one-dimensional array: find the sum, average, minimum, maximum, a count of elements meeting a condition, search for a value, and rearrange or shift elements. These build on the traversal pattern from Topic 6.2, adding an accumulator or a "running best" variable. They appear constantly in both multiple-choice and free-response questions, so the patterns should be automatic.

Sum and average

int sum = 0;
for (int i = 0; i < arr.length; i++) {
  sum = sum + arr[i];
}
double avg = (double) sum / arr.length;

The accumulator sum starts at 0 and grows by each element. For the average, cast to double so the division is floating-point, not truncating integer division (Unit 1).

Maximum and minimum

int max = arr[0];
for (int i = 1; i < arr.length; i++) {
  if (arr[i] > max) {
    max = arr[i];
  }
}
// count elements greater than 10
int count = 0;
for (int i = 0; i < arr.length; i++) {
  if (arr[i] > 10) {
    count++;
  }
}

// find index of first element equal to target, or -1
int index = -1;
for (int i = 0; i < arr.length; i++) {
  if (arr[i] == target && index == -1) {
    index = i;
  }
}

A counter accumulates a number of matches; a search records a position, using -1 as the "not found" sentinel.

Rearranging elements

Some questions ask you to shift or swap elements. A swap needs a temporary variable so a value is not overwritten before it is moved:

int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

Try this

Q1. Why should a maximum-finding algorithm initialise its "best" variable to arr[0] rather than to 0? [1 point]

  • Cue. If all elements are negative, starting at 0 would leave 0 as the maximum even though it is not in the array; arr[0] is guaranteed to be a real element.

Q2. Write a loop that counts how many elements of an int array arr are equal to 0. [2 points]

  • Cue. int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] == 0) { count++; } } - a counter incremented for each matching element.

Exam-style practice questions

Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AP 2022 (style)1 marksMultiple choice. Consider the following code segment, which is intended to store the largest element of `arr` in `max`. ```java int[] arr = {3, 9, 2, 9, 5}; int max = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } } ``` After the loop, what is the value of `max`? (A) `9` (B) `3` (C) `5` (D) `2` (E) `18`
Show worked answer →

The answer is (A).

max is initialised to arr[0] (3), then the loop compares each later element and keeps the larger. It updates to 9 at index 1, stays 9 (the second 9 is not greater), and 2 and 5 are smaller. So max ends at 9. (B) is the starting value; (C) and (D) are smaller elements; (E) would sum two values, which is not what the code does.

Markers reward the running-maximum pattern: initialise to the first element, then update whenever a larger element is found.

AP 2021 (style)4 marksFree response (code writing). An `int` array `arr` has been declared and initialised with at least one element. Write a code segment that computes and prints the average of the elements as a `double`. For example, for `{1, 2, 4}` the output should be approximately `2.3333333333333335`.
Show worked answer →

A 4-point question combining a sum accumulator with a careful cast to avoid integer division.

int sum = 0;
for (int i = 0; i < arr.length; i++) {
  sum = sum + arr[i];
}
double average = (double) sum / arr.length;
System.out.println(average);

Point 1: initialise the accumulator sum to 0. Point 2: a full traversal adding every element. Point 3: cast to avoid integer division - (double) sum / arr.length makes the division floating-point. Point 4: print the double average. Writing sum / arr.length without the cast would truncate to an int and lose the fraction.

Related dot points

Sources & how we know this