Arithmetic Slices II - Subsequence - Problem
Count All Arithmetic Subsequences
Given an integer array
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:
•
•
•
Goal: Return the total count of all arithmetic subsequences of length ≥ 3.
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 -5Goal: 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
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)
⚠ Quadratic Growth
Space Complexity
O(n²)
In worst case, each position stores hashmaps with O(n) differences
⚠ 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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code