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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 and search
// 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
- Topic 6.2 Traversing Arrays: use a standard for loop with an index variable to traverse an array, reading or modifying each element, while staying inside the valid index bounds.
A focused answer to AP CSA Topic 6.2, covering how to traverse a one-dimensional array with a standard indexed for loop, the correct loop bounds, reading versus modifying elements, partial traversals, and avoiding off-by-one errors and out-of-bounds exceptions, with a fully worked example.
- Topic 6.1 Array Creation and Access: declare and initialise a one-dimensional array, access elements by index from 0 to length minus 1, and use the length attribute.
A focused answer to AP CSA Topic 6.1, covering how to declare and create a one-dimensional array, default element values, the length attribute, accessing and modifying elements by index, the valid index range, and ArrayIndexOutOfBoundsException, with a fully worked example.
- Topic 6.3 Enhanced for Loop for Arrays: use an enhanced for-each loop to traverse the elements of an array, and explain why it cannot modify the array or access indices.
A focused answer to AP CSA Topic 6.3, covering the enhanced for-each loop syntax for arrays, how the loop variable holds a copy of each element, why it cannot change array elements or give the index, and when to use it versus a standard indexed loop, with a fully worked example.
- Topic 4.3 Developing Algorithms Using Strings: traverse a String with a loop using length, substring and indexOf to implement standard algorithms such as counting characters, searching for a pattern and building a new String.
A focused answer to AP CSA Topic 4.3, covering String traversal with a for loop, extracting one character with substring, the standard counting, searching and accumulation patterns, the half-open index range, and bounds safety, with a fully worked example.
- Topic 1.5 Casting and Ranges of Variables: use casting to convert between int and double, predict the effect of truncation, and recognize that an int has a finite range that can overflow.
A focused answer to AP CSA Topic 1.5, covering explicit casting between int and double, the truncation that casting to int causes, where the cast applies in an expression, the finite range of an int, and integer overflow, with a traced worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)