Arithmetic Slices II - Subsequence

Hard

Description

An arithmetic subsequence has at least 3 elements with constant difference. Return the number of arithmetic subsequences in the array.

Examples

Input:nums = [2,4,6,8,10]
Output:7
Explanation:

7 arithmetic subsequences exist.

Input:nums = [1]
Output:1
Explanation:

Edge case with a single-element array.

Input:nums = [7,7,7,7]
Output:4
Explanation:

All elements are equal, so the common difference is 0. Arithmetic subsequences of length 3+: [7,7,7] at positions (0,1,2), (0,1,3), (0,2,3), (1,2,3), and [7,7,7,7] using all positions. Total: 5.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • -2³¹ ≤ nums[i] ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!