1218. Longest Arithmetic Subsequence of Given Difference
class Solution:
def longestSubsequence(self, arr: List[int], difference: int) -> int:
n = len(arr)
if n == 1:
return 1
M = defaultdict(lambda: 0)
res = 0
for i in range(n):
M[arr[i]] = M[arr[i] - difference] + 1
res = max(res, M[arr[i]])
return res