Introduction
Topic 4.5 builds on array traversal skills by implementing standard algorithms that solve common problems. These algorithms appear frequently on the AP exam and form the building blocks for more complex programs.
Welcome to the FiveHive website article for topic 4.5!
Today, our learning targets, according to AP® Computer Science A Course and Exam Description 2025, are as follows:
4.5.A Develop code for standard and original algorithms for a particular context or specification that involves arrays and determine the result of these algorithms.
No content today… BUT still stick around because we’re going to be walking through various array-based algorithms for various important functions.
You can use these algorithms to do any of the following tasks:
- determine a minimum or maximum value
- compute a sum or average
- determine if at least one element has a particular property
- determine if all elements have a particular property
- determine the number of elements having a particular property
- access all consecutive pairs of elements
- determine the presence or absence of duplicate elements
- shift or rotate elements left or right
- reverse the order of the elements
Let’s jump in!
This topic covers nine categories of standard array algorithms: finding extremes, computing statistics, checking properties, working with consecutive pairs, detecting duplicates, and modifying array structure through shifting, rotating, and reversing. Mastering these patterns prepares you for both multiple-choice questions and free-response problems.
Finding Minimum and Maximum Values
Theory: Traverse the array, tracking the smallest or largest value encountered.
Actual:
// Find maximum
public static int findMax(int[] arr)
{
int max = arr[0];
for (int i = 1; i < arr.length; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}
// Find minimum
public static int findMin(int[] arr)
{
int min = arr[0];
for (int i = 1; i < arr.length; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
}
return min;
}Notice the common thread: initialize to first element, compare each subsequent element, update when finding a more extreme value.
Computing Sum and Average
Theory: Accumulate values as you traverse.
Actual:
// Calculate sum
public static int sum(int[] arr)
{
int total = 0;
for (int num : arr)
{
total += num;
}
return total;
}
// Calculate average
public static double average(int[] arr)
{
int total = 0;
for (int num : arr)
{
total += num;
}
return (double) total / arr.length;
}
The average formula:
Notice the common thread: Initialize accumulator to 0, add each element, return result (possibly divided by count).
Checking Element Properties
Three variations: at least one, all, or count.
At Least One Element Has Property
// Check if any element is negative
public static boolean hasNegative(int[] arr)
{
for (int num : arr)
{
if (num < 0)
{
return true; // Found one, stop immediately
}
}
return false; // None found
}Notice the common thread: Return as soon as condition is met; return if you finish without finding any.
All Elements Have Property
// Check if all elements are positive
public static boolean allPositive(int[] arr) {
for (int num : arr) {
if (num <= 0) {
return false; // Found violation, stop
}
}
return true; // No violations found
}
Notice the common thread: Return as soon as condition is violated; return if you finish without violations.
Count Elements With Property
// Count even numbers
public static int countEven(int[] arr)
{
int count = 0;
for (int num : arr)
{
if (num % 2 == 0)
{
count++;
}
}
return count;
}Pattern: Initialize counter to 0, increment when condition is met, return count.
Accessing Consecutive Pairs
Theory: Use loop from to safely access pairs ().
Actual:
// Count increases between consecutive elements
public static int countIncreases(int[] arr)
{
int count = 0;
for (int i = 0; i < arr.length - 1; i++)
{
if (arr[i + 1] > arr[i])
{
count++;
}
}
return count;
}
IMPORTANT: Loop condition must be to avoid accessing .
Detecting Duplicates
Theory: Use nested loops to compare each element with all subsequent elements.
Actual:
// Check if array contains any duplicates
public static boolean hasDuplicates(int[] arr)
{
for (int i = 0; i < arr.length; i++)
{
for (int j = i + 1; j < arr.length; j++)
{
if (arr[i] == arr[j])
{
return true;
}
}
}
return false;
}
Notice the common thread: Outer loop selects element, inner loop (starting at ) compares with remaining elements.
Shifting Elements
Shifting left: Remove first element, all others move left.
// Shift all elements left (first element is lost)
public static int[] shiftLeft(int[] arr)
{
int[] result = new int[arr.length];
for (int i = 0; i < arr.length - 1; i++)
{
result[i] = arr[i + 1];
}
// Last element becomes 0 (default value)
return result;
}Shifting right: Insert new element at start, last element is lost.
// Shift all elements right (last element is lost)
public static int[] shiftRight(int[] arr, int value)
{
int[] result = new int[arr.length];
result[0] = value;
for (int i = 1; i < arr.length; i++)
{
result[i] = arr[i - 1];
}
return result;
}Rotating Elements
Rotating left: First element wraps to end.
// Rotate left: first element moves to end
public static void rotateLeft(int[] arr)
{
if (arr.length <= 1) return;
int first = arr[0];
for (int i = 0; i < arr.length - 1; i++)
{
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = first;
}Rotating right: Last element wraps to beginning.
// Rotate right: last element moves to start
public static void rotateRight(int[] arr)
{
if (arr.length <= 1) return;
int last = arr[arr.length - 1];
for (int i = arr.length - 1; i > 0; i--)
{
arr[i] = arr[i - 1];
}
arr[0] = last;
}Difference from shifting: Rotations preserve all elements; shifts lose one element.
Reversing Arrays
Theory: Swap elements from both ends moving toward the middle.
Actual:
// Reverse array in place
public static void reverse(int[] arr)
{
for (int i = 0; i < arr.length / 2; i++)
{
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
}
Notice the common thread: Loop to , swap element at with element at .
Example: becomes
Now let's practice!
