Unit 4 Cycle 2 Day 27: Comprehensive: Counting Ascending Steps

Unit 4 Advanced (Cycle 2) Day 27 of 28 Advanced

Comprehensive: Counting Ascending Steps

Section Mixed — Review: All Unit 4

Key Concept

Counting ascending steps in an array identifies positions where each element is greater than its predecessor. The algorithm compares arr[i] to arr[i - 1] (starting from index 1) and increments a counter when the current element is larger. This is a variation of the adjacent comparison pattern. The AP exam may extend this to finding the longest ascending subsequence, counting descending steps, or identifying all peak elements (greater than both neighbors). Each variation requires slight modifications to the comparison logic.

Consider the following code segment.

int[] arr = {8, 3, 5, 1, 9, 2, 7, 4, 6}; int count = 0; for (int i = 1; i < arr.length; i++) { if (arr[i] > arr[i - 1]) count++; } System.out.println(count);

What is printed?

Answer: (A) 4

Ascending pairs: (3,5), (1,9), (2,7), (4,6). Count = 4.

Why Not the Others?

(B) Only 4 pairs are ascending.

(C) Missing one ascending pair.

(D) 8 is total comparisons, not ascending count.

Common Mistake

Counting ascending steps: compare arr[i] with arr[i-1]. Start loop at 1. Count when current > previous.

AP Exam Tip

Adjacent-pair counting: loop from 1, compare with previous. Counts properties of consecutive pairs.

Review this topic: Section Mixed — Review: All Unit 4 • Unit 4 Study Guide
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.