Hard

Quiz

#446 Arithmetic Slices II - Subsequence

APPROACH

Given an integer array nums, count all distinct arithmetic subsequences with at least 3 elements. A subsequence is arithmetic when adjacent pairs all share the same difference (e.g., [1,3,5,7,9], [7,7,7,7]). Subsequences preserve relative index order and duplicates are not double-counted.

The answer fits in a 32-bit integer.

Example 1:

Input: nums = [2,4,6,8,10]
Output: 7
Explanation: All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]

Example 2:

Input: nums = [7,7,7,7,7]
Output: 16
Explanation: Any subsequence of this array is arithmetic.
1 of 4
1:00

What is the optimal approach for this problem?