Arithmetic Slices II - Subsequence - Problem
Count All Arithmetic Subsequences

Given an integer array nums, your task is to count all possible arithmetic subsequences that can be formed from the array. An arithmetic subsequence is a sequence of at least 3 elements where the difference between consecutive elements is constant.

What makes this challenging:
• We're looking for subsequences (not subarrays) - elements don't need to be adjacent
• We need to count ALL possible arithmetic subsequences, not just find one
• The common difference can be any integer (positive, negative, or zero)

Examples of arithmetic sequences:
[1, 3, 5, 7, 9] - difference of 2
[7, 7, 7, 7] - difference of 0
[10, 5, 0, -5] - difference of -5

Goal: Return the total count of all arithmetic subsequences of length ≥ 3.

Input & Output

example_1.py — Basic Arithmetic Sequence
$ Input: [2, 4, 6, 8, 10]
Output: 7
💡 Note: The arithmetic subsequences are: [2,4,6], [4,6,8], [6,8,10], [2,4,6,8], [4,6,8,10], [2,4,6,8,10] (6 total) + [2,6,10] which is NOT arithmetic, so we have exactly 7 valid subsequences with difference 2.
example_2.py — Array with Duplicates
$ Input: [7, 7, 7, 7, 7]
Output: 16
💡 Note: All elements are the same, so difference = 0. For n=5 identical elements, we can choose any 3 elements (C(5,3)=10), any 4 elements (C(5,4)=5), or all 5 elements (C(5,5)=1). Total: 10+5+1=16 arithmetic subsequences.
example_3.py — Mixed Differences
$ Input: [1, 2, 3, 4]
Output: 3
💡 Note: Found arithmetic subsequences: [1,2,3] (diff=1), [2,3,4] (diff=1), and [1,2,3,4] (diff=1). Total of 3 subsequences, all with common difference 1.

Visualization

Tap to expand
🎵 Finding Musical Scales (Arithmetic Subsequences)2Note 14Note 26Note 38Note 410Note 5+2+2+2+2🎼 Musical Scales Found (Arithmetic Subsequences):[2,4,6] - Scale 1[4,6,8] - Scale 2[6,8,10] - Scale 3[2,4,6,8] - Scale 4[4,6,8,10] - Scale 5[2,4,6,8,10] - Scale 6Total: 6 musical scales found! 🎯🧠 DP Algorithm - How it works:1. For each note position, track how many scales can end there2. When adding note i, check all previous notes j < i3. If interval matches previous scales ending at j, extend them!4. Count new valid scales (length ≥ 3) as they form⚡ Time Complexity: O(n²) | Space Complexity: O(n²)Much better than brute force O(2^n) - we avoid generating all subsequences!
Understanding the Visualization
1
Scan Each Note Position
For each note (array element), consider it as the end of potential musical scales
2
Look Back at Previous Notes
Check all previous notes to see what musical intervals we can create
3
Extend Existing Scales
If we find matching intervals from previous positions, extend those scales
4
Count Valid Scales
Track all scales with 3+ notes that maintain consistent intervals
Key Takeaway
🎯 Key Insight: Instead of generating all possible subsequences (exponential time), we use dynamic programming to build arithmetic subsequences incrementally, tracking how many end at each position with each possible difference.

Time & Space Complexity

Time Complexity
⏱️
O(n²)

Nested loop through all pairs of elements, hashmap operations are O(1)

n
2n
Quadratic Growth
Space Complexity
O(n²)

In worst case, each position stores hashmaps with O(n) differences

n
2n
Quadratic Space

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -231 ≤ nums[i] ≤ 231 - 1
  • The answer is guaranteed to fit in a 32-bit integer
  • Array elements can be negative, positive, or zero
  • Arithmetic subsequences must have at least 3 elements
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
89.4K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen