Arithmetic Slices II - Subsequence

Problem Id: 446 Difficulty: Hard


Intuition

Solution


class Solution:
    def numberOfArithmeticSlices(self, sequence):
        n = len(sequence)
        count = 0
        prev_count = defaultdict(int)
        for i in range(n):
            candidates = Counter(sequence[i + 1])
            for j in range(i + 1, n):
                candidates[sequence[j]] -= 1
                count += candidates[sequence[j] + (sequence[j] - sequence[i])]
        return count