Counting alternating numbers in an array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
"Alternating" in an array usually means the comparison direction flips at each step, such as up, down, up, down or down, up, down, up. The important part is to define exactly what you want to count, because counting alternating pairs, alternating subarrays, and the longest alternating run are related but different problems.
Define Alternation Clearly
For a sequence a[i], an alternating contiguous segment is typically one where consecutive comparisons change sign:
- '
a[i] < a[i+1] > a[i+2] < a[i+3]' - or
a[i] > a[i+1] < a[i+2] > a[i+3]
Equal adjacent values usually break alternation because the direction is neither positive nor negative.
For example, in:
the comparisons are:
so the whole array is alternating.
Counting Alternating Subarrays in Linear Time
A useful interpretation is: count how many contiguous subarrays of length at least two are alternating.
The key observation is that if the current element extends an alternating run, then every alternating subarray ending at that position can be counted from the previous run length.
This runs in O(n) time and O(1) extra space.
Why the Formula Works
Suppose an alternating run currently has length L. Then the number of alternating subarrays ending at the current position is L - 1, because you can take:
- the last 2 elements
- the last 3 elements
- and so on up to the full run
So each step only needs to know whether the run continues or resets.
Counting Only Maximal Alternating Runs
Sometimes the task is not "count all alternating subarrays" but "count how many alternating segments exist." That is a different result.
That example shows why you must define the counting target before writing the algorithm.
Equal Values Need a Rule
If the array contains equal adjacent values, most definitions treat them as breaking alternation:
The step from 3 to 3 is flat, so the alternating pattern stops there. If your domain wants to ignore equal values instead, the algorithm must be adjusted deliberately.
Common Pitfalls
The biggest pitfall is failing to specify what should be counted. "Alternating numbers" can mean alternating subarrays, maximal runs, or just whether the whole array alternates.
Another common mistake is forgetting that equal adjacent elements usually break the sign pattern. If you do not handle that case, the count is often wrong.
Developers also overcomplicate the problem with nested loops. For contiguous alternation, a single pass is enough because each new element either extends or resets the current run.
Summary
- Alternation usually means consecutive comparisons flip direction.
- Counting alternating subarrays can be done in one pass with constant extra space.
- The same array can produce different answers depending on whether you count all subarrays or only maximal alternating runs.
- Equal adjacent values typically break alternation.
- Define the exact counting rule before choosing the algorithm.
Related reading
- counting boolean parenthesizations implementation
- Counting inversions in an array
- Counting inversions in ranges
- Counting Inversions using BIT
- Counting array elements in Python
- Counting subsets with given sizes of a set
- Counting minimum number of swaps to group characters in string
- Counting number of bits How does this line work ? nnn-1;

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.