Given an array A of integers, return the length of the longest arithmetic subsequence in A.
Recall that a subsequence of A is a list A[i_1], A[i_2], ..., A[i_k] with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1, and that a sequence B is arithmetic if B[i+1] - B[i] are all the same value (for 0 <= i < B.length - 1).
 
Example 1:
Input: A = [3,6,9,12]
Output: 4
Explanation: 
The whole array is an arithmetic sequence with steps of length = 3.

Example 2:
Input: A = [9,4,7,2,10]
Output: 3
Explanation: 
The longest arithmetic subsequence is [4,7,10].

Example 3:
Input: A = [20,1,15,3,10,5,8]
Output: 4
Explanation: 
The longest arithmetic subsequence is [20,15,10,5].

 
Constraints:

2 <= A.length <= 1000
0 <= A[i] <= 500

Difficulty: interview

Starter code:

class Solution:
    def longestArithSeqLength(self, A: List[int]) -> int:
        

Solutions:
["from collections import Counter\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        c = dict(Counter(A).most_common())\n        # print(c)\n        m1 = max(c.values())\n        # A = list(set(A))\n        # A.sort()\n        index = {}\n        # for i in range(len(A)):\n            # index[A[i]]=i\n        dp = [[2] * len(A) for i in A]\n        m = 2\n        for i in range(len(A)):\n            # print(\\\"I=\\\", i)\n            # index[A[i+1]]=(i+1)\n            for j in range(i+1, len(A)):\n                # index[A[j]]=(j)\n                a = A[i]\n                \n                c = A[j]\n                b = 2 * a - c\n                # print(b,a,c)\n                if b in index :\n                    # print(\\\"B {} in index \\\".format(b))\n                    # print(b,a,c,i,j)\n                    dp[i][j] = dp[index[b]][i] + 1\n            index[A[i]]=i\n            m = max(m, max(dp[i]))\n        # # print(A)\n        # for i,d in enumerate(dp):\n        #     print(A[i],d)\n        return max(m,m1)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = []\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleftl.append(val)\n                onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        \n        dp = [[2 for c in range(n)] for r in range(n)]\n        \n        visited = {}\n        res = 2\n        \n        for i in range(n - 1):\n            for j in range(i + 1, n):\n                \n                prev = A[i] * 2 - A[j]\n                \n                if prev < 0 or prev not in visited:\n                    continue\n                \n                dp[i][j] = dp[visited[prev]][i] + 1\n            \n                res = max(res, dp[i][j])\n            \n            visited[A[i]] = i\n        \n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res, n = 1, len(A)\n        dp = [{} for _ in range(n)]\n        for i in range(1, n):\n            for j in range(i-1, -1, -1):\n                d = A[i] - A[j]\n                if d in dp[i]: continue\n                if d in dp[j]:\n                    dp[i][d] = dp[j][d]+1\n                else:\n                    dp[i][d] = 2 \n                res = max(res, dp[i][d])\n        # print(dp)        \n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        maxValue = 1;\n        for i in range(len(A)):\n            for j in range(0, i):\n                dp[i, A[i] - A[j]] = dp.get((j, A[i] - A[j]), 0) + 1\n                maxValue = max(maxValue, dp[i, A[i] - A[j]])\n        return maxValue + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp=[dict() for a in A]\n        for idx, a in enumerate(A):\n            for j in range(idx):\n                diff=a-A[j]\n                dp[idx][diff]=dp[j].get(diff,1)+1\n        \n        def get_len(d):\n            if not d:\n                return 0\n            return max(d.values())\n        \n        return max(map(get_len,dp))", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        a = len(A)\n        dp = [[0]*a for _ in range(a)] # dp array\n        index = [-1]*20001#index array\n        maximum = 2\n        for i in range(0,a):\n            dp[i] = [2]*a\n            for j in range(i+1, a):\n                first = A[i]*2-A[j]\n                if first < 0 or index[first]==-1:\n                    continue\n               \n                dp[i][j] = dp[index[first]][i]+1\n                maximum = max(maximum,dp[i][j] ) \n                \n            index[A[i]] = i\n        return maximum\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        if len(A) == 1 or len(A) == 2:\n            return len(A)\n        D = [dict() for _ in range(len(A))]\n        ans = 0\n        for i, a in enumerate(A[1:], 1):\n            for j in range(i):\n                if a - A[j] not in D[j]:\n                    D[i][a - A[j]] = 2\n                else:\n                    D[i][a - A[j]] = D[j][a - A[j]] + 1\n            ans = max(ans, max(D[i].values()))\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        if len(A) < 3:\n            return len(A)\n        \n        dp = [ {} for i in range(len(A))]\n        m = 2\n        \n        for i in range(1, len(A)):\n            for j in range(i):  # here we have to iterate from 0 to i-1 and not i-1 to 0.\n                \n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff]+1\n                    if m < dp[i][diff]:\n                        m = dp[i][diff]\n                else:\n                    dp[i][diff] = 2\n\n        return m\n                \n                \n                \n                \n                \n                \n                \n                \n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        a = len(A)\n        dp = [[0]*a for _ in range(a)]\n        index = [-1]*20001\n        maximum = 2\n        for i in range(0,a):\n            dp[i] = [2]*a\n            for j in range(i+1, a):\n                first = A[i]*2-A[j]\n                if first < 0 or index[first]==-1:\n                    continue\n                else:\n                    dp[i][j] = dp[index[first]][i]+1\n                    maximum = max(maximum,dp[i][j] ) \n            index[A[i]] = i\n        return maximum\n    \n    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        \n        dp = [{} for _ in range(n)]\n        \n        ans = 0\n        \n        for i in range(n):\n            dp[i][0] = 1\n            for j in range(i):\n                diff = A[i] - A[j]\n                \n                if diff not in dp[j]:\n                    dp[i][diff] = 2\n                else:\n                    dp[i][diff] = dp[j][diff] + 1\n            \n            ans = max(ans, max([dp[i][key] for key in dp[i]]))\n        \n        return ans", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        highest = 0\n        offsets = [None] * len(A)\n        for i in range(len(A)):\n            offsets[i] = defaultdict(int)\n        for i in range(len(A) - 1,-1,-1):\n            # go forwards\n            for j in range(i,-1,-1):\n                offset = A[i] - A[j]\n                if offset == 0:\n                    continue\n                offsets[i][offset] = 1\n            # go backwards\n            seen_offsets = set()\n            for j in range(i,len(A)):\n                offset = (A[i] - A[j]) * -1\n                if offset == 0 or offset in seen_offsets:\n                    continue\n                seen_offsets.add(offset)\n                # increment only for the first time we've seen this offset going back\n                offsets[i][offset] += offsets[j][offset]\n                if offsets[i][offset] > highest:\n                    highest = offsets[i][offset]\n        #for offset in offsets:\n        #    print(offset)\n        return highest + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int: \n        dp = []\n        \n        for i, x in enumerate(A):\n            nd = collections.defaultdict(int)\n            dp.append(nd)\n            for j in range(i):\n                curr_diff = x - A[j]\n                dp[i][curr_diff] = dp[j][curr_diff] + 1\n          \n        return max(max(y.values()) for y in dp) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        ALen = len(A)\n        dictList = [defaultdict(lambda: 1) for _ in range(ALen)]\n        ans = 2\n        for i, num in enumerate(A):\n            for j in range(i-1, -1, -1):\n                delta = A[i] - A[j]\n                if delta not in dictList[i]:\n                    dictList[i][delta] = 1 + dictList[j][delta]\n                    ans = max(ans, dictList[i][delta])\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        from collections import defaultdict\n\n        d = [{} for _ in range(len(A))]\n        res = 2\n\n        for i, x in enumerate(A):\n            for j in range(i):\n                diff = x - A[j]\n                if diff in d[j]:\n                    d[i][diff] = d[j][diff] + 1\n                    # d[j].pop(diff)\n                    \n                    res = max(res, d[i][diff])\n                    \n                else:\n                    d[i][diff] = 2\n\n                \n        return res\n\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = []\n        \n        for i, x in enumerate(A):\n            nd = collections.defaultdict(int)\n            dp.append(nd)\n            for j in range(i):\n                curr_diff = x - A[j]\n                dp[i][curr_diff] = dp[j][curr_diff] + 1\n          \n        return max(max(y.values()) for y in dp) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n\n        if n <= 1:\n            return n \n        \n        ap = [None] * n\n        for i in range(n):\n            ap[i] = dict()\n\n        for j in range(1, n):\n            for i in range(0, j):\n                diff = A[j] - A[i]\n                ap[j][diff] = ap[i].get(diff, 1) + 1\n\n        ans = 0\n\n        for item in ap[1:]:\n            vals = max(item.values())\n            ans = max(ans, vals)\n\n        return ans ", "class Solution:\n    def longestArithSeqLength(self, nums: List[int]) -> int:\n        n = len(nums)\n        \n        if n == 2:\n            return n\n        \n        dp = [{} for i in range(n)]\n        res = 0\n        for i in range(1, n):\n            for j in range(i):\n                dis = nums[i] - nums[j]\n                # \u5728\u524d\u9762\u7684dp[j]\u4e2d \u7528get()\u5bfb\u627edp\u4e2d\u5df2\u6709\u7684\u6570\u636e\u3002\n                # \u5982\u679c\u6ca1\u6709\uff0c\u8bf4\u660e\u662f\u57fa\u672c\u72b6\u6001\uff0c\u75281\u521d\u59cb\u5316\uff0c+1\u4e3a2\n                x = dp[j].get(dis, 1)+1\n                dp[i][dis] = x\n            res = max(res, max(dp[i].values()))\n\n        return res\n\n", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        dp = [defaultdict(lambda: 1) for _ in range(N)]\n        for i in range(N):\n            for j in range(i+1, N):\n                diff = A[j] - A[i]\n                dp[j][diff] = dp[i][diff] + 1 \n        return max([max(d.values()) for d in dp])", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = []\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleft:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleftl.append(val)\n                onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        memo = [{} for _ in range(len(A))] \n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                memo[i][diff] = 1 + memo[j].get(diff, 1)\n                #result = max(result, memo[i][diff])\n        return max(d[diff] for d in memo for diff in d)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        table = []\n        for i, z in enumerate(A):\n            table.append(collections.defaultdict(lambda: 1))\n            for j in range(i):\n                diff = z - A[j]\n                table[i][diff] = table[j][diff] + 1\n        \n        return max(max(y.values()) for y in table)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int: \n        dp = []\n        \n        for i, x in enumerate(A):\n            nd = collections.defaultdict(int)\n            dp.append(nd)\n            for j in range(i):\n                curr_diff = x - A[j]\n                dp[i][curr_diff] = dp[j][curr_diff] + 1\n          \n        return max(max(y.values()) for y in dp) + 1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = dict()\n\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff not in dp:\n                    dp[diff] = {i: 2}\n                else:\n                    dic = dp[diff]\n                    if j in dic:\n                        dic[i] = dic[j] + 1\n                    else:\n                        dic[i] = 2\n\n        return max(max(v1 for k1, v1 in v.items()) for k, v in dp.items())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A)<2: return len(A)\n        table = []\n        result = 1\n        \n        for i, z in enumerate(A):\n            table.append(collections.defaultdict(lambda: 1))\n            for j in range(i):\n                diff = z - A[j]\n                table[i][diff] = table[j][diff] + 1\n                #if table[i][diff] > result: result = table[i][diff]\n        \n        return max([max(y.values()) for y in table])", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int: \n        dp = []\n        \n        for i, x in enumerate(A):\n            nd = collections.defaultdict(lambda: 1)\n            dp.append(nd)\n            for j in range(i):\n                curr_diff = x - A[j]\n                dp[i][curr_diff] = dp[j][curr_diff] + 1\n          \n        return max(max(y.values()) for y in dp)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        cache = [{} for i in range(len(A))]\n        \n        n = len(A)\n        for i in range(1,n):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff not in cache[j]:\n                    cache[i][diff] = 2\n                else:\n                    cache[i][diff] = cache[j][diff] + 1\n\n        m = 0\n        for dictionary in cache:\n            if dictionary:\n                m = max(m, max(dictionary.values()))\n        return m", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        # out = [1] * n\n        outdict = {}\n        longseq = 0\n        for i in range(n):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                if diff not in outdict:\n                    outdict[diff] = [1] * n\n                pointer = outdict[diff] \n                pointer[i] = max(pointer[i], pointer[j] + 1)\n                longseq = max(longseq, pointer[i])\n        # print(longseq)\n        return longseq\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        memo = [{} for _ in range(len(A))] \n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                memo[i][diff] = 1 + memo[j].get(diff, 1)\n        return max(d[diff] for d in memo for diff in d)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = {}\n        dd = [{} for i in range(len(A))]\n        m = 0\n        for i in range(len(A)):\n            \n            for j in range(i + 1, len(A)):\n                diff = A[j] - A[i]\n                #if diff not in d:\n                #    d[diff] = 0\n                if diff not in dd[i]:\n                    dd[j][diff] = 1\n                else:\n                    dd[j][diff] = dd[i][diff] + 1\n                    \n                if dd[j][diff] > m:\n                    m = dd[j][diff]\n                \n                #d[diff] += 1\n        #if not d:\n        #    return 0\n        #print(d)\n        #return max(d.values()) + 1\n        return m + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = []\n        onright = [0] * 501 #[0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleftl.append(val)\n                onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = dict()\n\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff not in dp:\n                    dp[diff] = {i: 2}\n                else:\n                    dic = dp[diff]\n                    if j in dic:\n                        dic[i] = dic[j] + 1\n                    else:\n                        dic[i] = 2\n\n        return max(max(v1 for k1, v1 in list(v.items())) for k, v in list(dp.items()))\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        cache = [{} for i in range(len(A))]\n        m = 0\n\n        n = len(A)\n        for i in range(1,n):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff not in cache[j]:\n                    cache[i][diff] = 2\n                else:\n                    cache[i][diff] = cache[j][diff] + 1\n                if cache[i][diff] > m:\n                    m = cache[i][diff]\n\n        return m", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [{} for _ in range(len(A))]\n        maxSequence = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i][diff] = val = dp[j].get(diff,1) + 1\n                if val > maxSequence:\n                    maxSequence = val\n        return maxSequence", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        for i in range(len(A)):\n            nd = collections.defaultdict(int)\n            dp.append(nd)\n            for j in range(i):\n                curr_diff = A[i] - A[j]\n                dp[i][curr_diff] = dp[j][curr_diff] + 1\n        maxVal = -99999\n        for dt in dp:\n            maxVal = max(maxVal, max(dt.values()) + 1)\n        return maxVal", "class Solution(object):\n    def longestArithSeqLength(self, A):\n        if not A:\n            return None\n        dp = [{} for i in range(0,len(A))]\n        for i in range(len(A)):\n            if i == 0:\n                dp[i][0]= 1\n            else:\n                for j in range(0,i):\n                    diff = A[i]-A[j]\n                    if diff in dp[j]:\n                        dp[i][diff] = dp[j][diff]+1\n                    else:\n                        dp[i][diff] = 2\n        mx = 2\n        for j in range(len(A)):\n            for i in dp[j]:\n                mx = max(dp[j][i],mx)\n        return mx", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        \n        dp = []\n        for _ in range(len(A)):\n            dp.append(dict())\n        dp[0][0] = 1\n        \n        for i in range(1, len(A)):\n            dp[i][0] = 1\n            for j in range(i):\n                # continue subsequence\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    if diff not in dp[i]:\n                        dp[i][diff] = dp[j][diff] + 1\n                    else:\n                        dp[i][diff] = max(dp[i][diff], dp[j][diff] + 1)\n                    \n                # start new subsequence\n                else:\n                    dp[i][diff] = 2\n                    \n        # for x in dp:\n        #     print(str(x))\n        return max([max(x.values()) for x in dp])\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # subsequence problem -> dp\n        # dp[i][j] -- length of arithmetic subsequence ending at ith and jth element\n        ans = 2\n        n = len(A)\n        index = {}\n        dp = [[2] * n for i in range(n)]\n        \n        for i in range(n-1):\n            for j in range(i+1, n):\n                first = A[i] * 2 - A[j]\n                if first in index:\n                    dp[i][j] = dp[index[first]][i] + 1\n                    ans = max(ans, dp[i][j])\n            index[A[i]] = i\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        answer = 2\n        \n        L = len(A)\n        table = [dict() for _ in range(L)]\n        \n        for i in range(1, L):\n            for j in range(0, i):\n                diff = A[i] - A[j] \n                if not diff in table[j]:\n                    table[i][diff] = 2\n                else:\n                    \n                    table[i][diff] = table[j][diff] + 1\n                    answer = max(answer, table[i][diff])\n                \n\n        return answer", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[i][j]: for 0-ith elements, the length of subsquence when step = j\n        # dp[i][j] = dp[i-1][A[i]-A[k]] + 1 where k = 0, 1, ...i-1\n        # return max(dp[n-1][j])\n        # base case dp[0][0] = 1\n        \n        N = len(A)\n        dp = [{} for _ in range(N)]\n        for i in range(1, N):\n            for j in range(0, i):\n                dp[i][A[i]-A[j]] = dp[j].get(A[i]-A[j], 0) + 1\n        max_len = 0\n        for i in range(1, N):\n            max_len = max(max_len, max(dp[i].values()))\n        return max_len + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = [False for _ in range(501)]\n        onleftl = []\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if not onleft[val]:\n                onleftl.append(val)\n                onleft[val] = True\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        if len(A) < 2:\n            return len(A)\n        \n        dp = [ {} for i in range(len(A))]\n        m = 2\n        \n        for i in range(1, len(A)):\n            for j in range(0, i):\n                \n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff]+1\n                    m = max(m, dp[i][diff])\n                \n                else:\n                    dp[i][diff] = 2\n\n        return m\n                \n                \n                \n                \n                \n                \n                \n                \n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        for _ in range(len(A)):\n            dp.append({})\n        max_ = 0\n        dp[0][0] = 1\n        for i in range(1, len(A)):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                if dp[i][diff] > max_:\n                    max_ = dp[i][diff]\n        return max_", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        '''\n        9  4     7     2     10\n          -5     4 2  -5 2   8\n                -2 2  -2 3   3\n                      -7 2   6\n                             1\n           j      i \n        \n        \n        could sort array. \n        \n        could iterate through jump sizes\n        could transition each elem to distance from other.  \n        '''\n        if len(A) < 3:\n            return len(A)\n        \n        best = 2\n        sequences = [ {} for _ in A]\n        for right in range(1, len(A)):\n            for left in range(right):\n                diff = A[right] - A[left]\n                #print(diff, sequences[left])\n                if diff in sequences[left]:\n                    count = sequences[left][diff] + 1\n                    sequences[right][diff] = count\n                    best = max(best, count)\n                else: \n                    sequences[right][diff] = 2\n        \n        return best\n\n        \n        '''\n        \n        best = 2\n        for i in range(len(A)-2):\n            for j in range(len(A)-1):\n                jump = A[j] - A[i]\n                last = A[j]\n                thisCount = 2\n                for k in range(j+1, len(A)):\n                    if A[k] == last + jump:\n                        thisCount += 1\n                        last = A[k]\n                best = max(best, thisCount)\n        return best\n        '''\n                        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) == 1:\n            return 1\n        max_l = 2\n        dp = [{0: 1}]\n        for i in range(1, len(A)):\n            dp.append({})\n            for j in range(0, i):\n                idx = A[i] -A[j]\n                if idx in dp[j]:\n                    dp[i][idx] = dp[j][idx] + 1\n                    max_l = max(max_l, dp[i][idx])\n                else:\n                    dp[i][idx] = 2\n        return max_l", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        '''\n        # Note:\n            A.length >= 2\n        # Analysis:\n            Arithmetic sequence\n        \n        '''\n        n = len(A)\n        res = 2\n        dif_arr = [defaultdict(int) for _ in range(n)]\n        for i in range(1, n):\n            for j in range(i):\n                dif = A[i] - A[j]\n                if dif in dif_arr[j]:\n                    dif_arr[i][dif] = dif_arr[j][dif] + 1\n                    res = max(res, dif_arr[i][dif]+1)\n                else:\n                    dif_arr[i][dif] = 1\n                \n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        if len(A) < 2:\n            return len(A)\n        \n        dp = [ {} for i in range(len(A))]\n        m = 2\n        \n        for i in range(1, len(A)):\n            for j in range(0, i):\n                \n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff]+1\n                    m = max(m, dp[i][diff])\n                \n                else:\n                    if diff not in dp[i]:\n                        dp[i][diff] = 2\n\n        return m\n                \n                \n                \n                \n                \n                \n                \n                \n                \n                \n", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # mapping: idx -> (mapping: arithmetic_step -> longest_arithmetic_subsequence_with_this_step_ending_at_idx\n        d = defaultdict(lambda: defaultdict(lambda: 1))\n        \n        for i,a in enumerate(A):\n            # LAS: A[i]\n            d[i][0] = 1\n            for j in range(i):\n                # Consider each subsequence that ends at i ~        A[?] ... A[??] A[j] A[i]\n                # A[i] - A[j] denotes the step\n                # LSA(j, step) := length of LSA ending at j with progression equal to step\n                # We only care about count, not the actual sequence, so length of such subsequence will be: 1 + LSA(j, step)\n                step = A[i] - A[j]\n                d[i][step] = d[j][step] + 1\n        return max([max(dn.values()) for dn in d.values()])", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # d subproblem\n        # index, difference\n        D = []\n        for i in range(1001):\n            D.append([0]* 1002) # first 501 is pos, second 501 is neg difference\n        best = 0\n        for second in range(len(A)-1, -1, -1):\n            for first in range(second-1, -1, -1):\n                diff = A[second]-A[first]\n                if diff < 0:\n                    diff = 500 + -1 * diff\n                D[first][diff] = D[second][diff] + 1\n                if D[first][diff] > best:\n                    best = D[first][diff]\n                    # print(f'best: {best}, first: {first}, diff: {diff}')\n        # print(D[0][501+5])\n        return best + 1\n", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # mapping: \n        d = defaultdict(lambda: defaultdict(lambda: 1))\n        \n        for i,a in enumerate(A):\n            d[i][0] = 1\n            for j in range(i):\n                step = A[i] - A[j]\n                d[i][step] = d[j][step] + 1\n        return max([max(dn.values()) for dn in d.values()])", "from itertools import repeat\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = 0\n        onleftl = []\n        onright = {}\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] = onright.get(v, 0) + 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if onright.get(nextval, 0) == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            b = (1<<val)\n            if not (onleft & b):\n                onleftl.append(val)\n                onleft = (onleft | b)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        memo = {} # storing next Item -> (diff pattern, length so far)\n        # [a, b, c]\n        maxLength = 2\n        \n        if len(A) < 3:\n            return len(A)\n            \n        \n        for i in range(len(A)): # iterating over A\n            if A[i] in memo:\n                toIter = [(i, j) for i, j in list(memo[A[i]].items())]\n                del memo[A[i]]\n                for k in toIter:\n                    diff, length = k\n                    if length > maxLength:\n                        maxLength = length\n                    length += 1\n\n                    newKey = A[i] + diff\n                    if newKey not in memo:\n                        memo[newKey] = {}\n                    if diff in memo[newKey]:\n                        memo[newKey][diff] = max(length, memo[newKey][diff])\n                    else:\n                        memo[newKey][diff] = length\n            for j in range(i):\n                diff = A[i] - A[j]\n                newKey = A[i] + diff\n                if A[i] + diff not in memo:\n                    memo[newKey] = {}\n                if diff not in memo[newKey]:\n                    memo[newKey][diff] = 3\n            \n                    \n        return maxLength\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [defaultdict(int) for a in A]\n        for i,a in enumerate(A):\n            for j in range(i):\n                dp[i][a-A[j]]=dp[j][a-A[j]]+1\n        #print(dp)\n        m = 0\n        for d in dp:\n            x = d.values()\n            if x: m=max(m, max(x))\n        return m+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i in range(1, len(A)):\n            item = A[i]\n            for j in range(0, i):\n                d = item - A[j]\n                \n                if (j, d) in dp:\n                    dp[i, d] = dp[j, d] + 1\n                else:\n                    dp[i, d] = 2\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = collections.defaultdict(dict)\n        n = len(A)\n        res = 1\n        for i in range(n):\n            for j in range(i):\n                v = A[i] - A[j]\n                if v not in d[j]: d[j][v] = 1\n                if v not in d[i]: d[i][v] = 0\n                d[i][v] = max(d[i][v] ,d[j][v] + 1)\n                res = max(res, d[i][v])\n        return res", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = defaultdict(lambda: defaultdict(lambda: 1))\n        \n        for i,a in enumerate(A):\n            d[a][0] = 1\n            for j in range(i):\n                d[a][A[i] - A[j]] = d[A[j]][A[i]-A[j]] + 1\n        return max([max(dn.values()) for dn in d.values()])", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i, Ai in enumerate(A):\n            for j in range(i+1, len(A)):\n                b = A[j] - Ai\n                if (i,b) not in dp: dp[j,b] = 2\n                else              : dp[j,b] = dp[i,b] + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        def dp_matrix_based():\n            item_dict = collections.defaultdict(list)\n            for i, x in enumerate(A):\n                item_dict[x].append(i)\n\n            C = max(A) - min(A)\n            n = len(A)\n            maxlen = -math.inf\n            dp =[[-math.inf]*(2*C+1) for _ in range(n)]\n\n            for i in range(n):\n                dp[i][0 + C] = 1\n\n            for i in range(n):\n                for j in range(i+1, n):\n                    g = A[j] - A[i] + C \n                    dp[j][g] = 2\n\n            for i in range(1,n):\n                for gap in range(2*C+1):\n                    candidate = A[i] - gap + C\n                    if candidate in item_dict:\n                        for t in item_dict[candidate]:\n                            if t < i:\n                                dp[i][gap] = max(dp[i][gap], dp[t][gap] + 1)\n                                maxlen = max(maxlen, dp[i][gap])\n\n            return maxlen\n        \n        # return dp_matrix_based()\n        \n        def dict_based():\n            '''\n            Less space and simpler\n            '''\n            \n            dp = defaultdict(defaultdict)\n            for i in range(len(A)):\n                for j in range(i):\n                    diff = A[i] - A[j]\n                    if diff not in dp:\n                        #save an inner dictionary with the higher index\n                        dp[diff] = { i: 2 }\n                    else:\n                        dic = dp[diff]\n                        if j not in dic:\n                            dic[i] = 2\n                        else:\n                            dic[i] = dic[j] + 1\n            maxlen = 0\n            for k,v in list(dp.items()):\n                for k1, v1 in list(v.items()):\n                    maxlen = max(maxlen, v1)\n            return maxlen\n        return dict_based()\n            \n            \n", "from array import array\nfrom itertools import repeat\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = []\n        onright = array('H', repeat(0, 501))\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleftl.append(val)\n                onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        for _ in range(len(A)):\n            dp.append({})\n        max_ = 0\n        for i in range(len(A)):\n            for j in range(0, i):\n                if i == 0:\n                    dp[i][0] == 1\n                else:\n                    diff = A[i] - A[j]\n                    if diff in dp[j]:\n                        dp[i][diff] = dp[j][diff] + 1\n                    else:\n                        dp[i][diff] = 2\n                    if dp[i][diff] > max_:\n                        max_ = dp[i][diff]\n        return max_", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp = defaultdict(int)\n        dp = {}\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if (j,diff) in dp:\n                    dp[(i,diff)] = dp[(j,diff)] + 1\n                else:\n                    dp[(i,diff)] = 1\n        return max(dp.values()) + 1", "#dp\n#d[(i, diff)] = len: end at i with diff has maximum subsequence length len\n#i: right num idx, j: left num idx\n#d[(i, diff)] = d[(j, diff)] + 1 if (j, diff) in d else 2, j = 0...i-1\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = dict()\n        for i, a in enumerate(A):\n            for j in range(i):\n                diff = a - A[j]\n                if (j, diff) in d:\n                    d[(i, diff)] = d[(j, diff)] + 1\n                else:\n                    d[(i, diff)] = 2\n        return max(d.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        # Question: list is arithmetic with constant diff bet elements\n        # dict of dict -> for each index key have dict with diff as key and count as value\n        cur = collections.defaultdict(dict)\n        \n        maxSeq = 0\n        for i, v in enumerate(A):\n            for j in range(i):\n                val = v - A[j]\n                cur[i][val] = 1 + cur[j].get(val, 1)  # def is 1 and not 0 since first time its 2 no's diff\n                # print(cur)\n                if maxSeq < cur[i][val] : maxSeq = cur[i][val]\n                \n        \n        return maxSeq\n        # return max(cur[i][j] for i in cur for j in cur[i])\n        \n# [3,6,9,12]   \n# defaultdict(<class 'dict'>, {})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2, 3: 3}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2, 3: 3}, 3: {9: 2}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2, 3: 3}, 3: {9: 2, 6: 2}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2, 3: 3}, 3: {9: 2, 6: 2, 3: 4}})\n\n\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        N = len(A)\n        for i, n in enumerate(A):\n            for j in range(i+1, N):\n                b = A[j] - n\n                if (i, b) in dp:\n                    dp[j, b] = dp[i, b] + 1\n                else:\n                    dp[j, b] = 2\n                    \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        # Question: list is arithmetic with constant diff bet elements\n        # dict of dict -> for each index key have dict with diff as key and count as value\n        cur = collections.defaultdict(dict)\n        \n        maxSeq = 0\n        \n        # this is DP problem but this is one way to solve without DP, by storing all\n        for i, v in enumerate(A):\n            for j in range(i):\n                val = v - A[j]    # end -start\n                # diff cnt + previous diff cnt from start(j)\n                cur[i][val] = 1 + cur[j].get(val, 1)  # def is 1 and not 0 since first time its 2 no's diff\n                # print(cur)\n                if maxSeq < cur[i][val] : maxSeq = cur[i][val]\n                \n        \n        return maxSeq\n        # return max(cur[i][j] for i in cur for j in cur[i])\n        \n# [3,6,9,12]   \n# defaultdict(<class 'dict'>, {})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2, 3: 3}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2, 3: 3}, 3: {9: 2}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2, 3: 3}, 3: {9: 2, 6: 2}})\n# defaultdict(<class 'dict'>, {0: {}, 1: {3: 2}, 2: {6: 2, 3: 3}, 3: {9: 2, 6: 2, 3: 4}})\n\n\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [{} for _ in A]\n        max_l = 1\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i][diff] = dp[j].get(diff, 1) + 1\n                if dp[i][diff] > max_l:\n                    max_l = dp[i][diff]\n        return max_l\n", "from itertools import repeat\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = 0\n        onleftl = []\n        onright = {}\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] = onright.get(v, 0) + 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval not in onright or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            b = (1<<val)\n            if not (onleft & b):\n                onleftl.append(val)\n                onleft = (onleft | b)\n        return res", "import bisect\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = {}\n        \n        for i, num in enumerate(A):\n            for j in range(i+1, len(A)):\n                diff = A[j]-num\n                if (i, diff) not in dp:\n                    dp[(j, diff)] = 2\n                else:\n                    dp[(j, diff)] = dp[(i, diff)] + 1\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n\n    \n        f = {}\n        maxlen = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                #fff[(A[i], diff)] = max(fff[(A[i], diff)], fff.get((A[j], diff), 1) + 1)\n                #f[(i, diff)] = max(f[(i, diff)], f.get((j, diff), 1) + 1)\n                #f[(i, diff)] = f.get((j, diff), 1) + 1\n                \n                if (j, diff) not in f:\n                    f[i, diff] = 2\n                else:\n                    f[i, diff] = f[j, diff] + 1          \n                                    \n                #maxlen = max(maxlen, f[(i, diff)])\n\n        return max(f.values())\n", "from collections import OrderedDict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # When adding a new number A[j], we look at all previous numbers A[i]:\n        # (1) If A[j] can extend any arithmetic subsequence currently ends at A[i]: LAS += 1\n        # (2) Otherwise, LAS = 2\n        subseq_lengths = {}\n        for j in range(1, len(A)):\n            for i in range(j):\n                diff = A[j] - A[i]\n                if (diff, i) in subseq_lengths:\n                    subseq_lengths[diff, j] = subseq_lengths[diff, i] + 1\n                else:\n                    subseq_lengths[diff, j] = 2\n        return max(subseq_lengths.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        n = len(A)\n        d = {}\n        for i in range(n):\n            for j in range(i+1,n):\n                diff = A[j] - A[i]\n                \n                if (i,diff) in d:\n                    d[(j,diff)] = d[(i,diff)] + 1\n                    \n                else:\n                    d[(j,diff)] = 2\n                    \n        #print(d)\n        return max(d.values())\n    \n        # n=len(A)\n        # dp={}\n        # for i in range(n):\n        #     for j in range(i+1,n):\n        #         dif = A[j]-A[i]\n        #         if (i,dif) in dp :\n        #             dp[(j,dif)]=dp[(i,dif)]+1\n        #         else:\n        #             dp[(j,dif)]=2\n        # return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n=len(A)\n        if n<=1: return A\n        t=collections.defaultdict(int)\n        dp=[None]*n\n        for i in range(n):\n            dp[i]=collections.defaultdict(int)\n        for i in range(n):\n            for j in range(i):\n                diff=A[i]-A[j]\n                dp[i][diff]=dp[j][diff]+1\n                \n        ret=0\n        for i in range(n):\n            ret=max(ret,max(dp[i].values())+1)\n        return ret\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n=len(A)\n        dp={}\n        for i in range(n):\n            for j in range(i+1,n):\n                dif = A[j]-A[i]\n                if (i,dif) in dp :\n                    dp[(j,dif)]=dp[(i,dif)]+1\n                else:\n                    dp[(j,dif)]=2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A: return 0\n        if len(A)<3: return 2\n        dp = {}\n        for i, a1 in enumerate(A[1:], 1):\n            for j, a2 in enumerate(A[:i]):\n                diff = a1 - a2\n                if (j, diff) in dp:\n                    dp[i, diff] = dp[j, diff] + 1\n                else:\n                    dp[i, diff] = 2\n                    \n        return max(dp.values())\n                \n            \n        \n        \n        \n                        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = 0\n        onleftl = []\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                c = tex.get(diff, 2) + 1\n                if c > res:\n                    res = c\n                else:\n                    ending = (res - c) * diff + nextval\n                    if ending > 500 or ending < 0 or onright[ending] == 0:\n                        continue\n                toextend[nextval][diff] = c\n            b = (1 << val)\n            if not (onleft & b):\n                onleft = (onleft | b)\n                onleftl.append(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n#         d = {}\n#         for i in range(len(A)):\n#             for j in range(i+1,len(A)):\n#                 diff = A[j] - A[i]\n                \n#                 if (i,diff) in d:\n#                     d[(j,diff)] = d[(i,diff)] + 1\n                    \n#                 else:\n#                     d[(j,diff)] = 2\n                    \n#         print(d)\n#         return max(d.values())\n    \n        n=len(A)\n        dp={}\n        for i in range(n):\n            for j in range(i+1,n):\n                dif = A[j]-A[i]\n                if (i,dif) in dp :\n                    dp[(j,dif)]=dp[(i,dif)]+1\n                else:\n                    dp[(j,dif)]=2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        if len(A) <= 2:\n            return len(A)\n        \n        n = len(A)\n        memo = {}\n        \n        for i in range(n):\n            for j in range(i + 1,n):\n                diff = A[j] - A[i]\n                memo[(j, diff)] = memo[(i, diff)] + 1 if (i, diff) in memo else 2\n        \n        return max(memo.values())", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for endi, endv in enumerate(A[1:], start = 1):\n            for starti, startv in enumerate(A[:endi]):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n        \n\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i, a2 in enumerate(A[1:], start=1):\n            for j, a1 in enumerate(A[:i]):\n                d = a2 - a1\n                if (j, d) in dp:\n                    dp[i, d] = dp[j, d] + 1\n                else:\n                    dp[i, d] = 2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = {}\n        for i in range(1, n):\n            for j in range(i):\n                dif = A[i] - A[j]\n                if (j,dif) in dp:\n                    dp[(i, dif)] = dp[(j, dif)] + 1\n                else:\n                    dp[(i, dif)] = 2\n                #print(dp)\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i, a2 in enumerate(A[1:], start=1):\n            for j, a1 in enumerate(A[:i]):\n                d = a2 - a1\n                if (j, d) in dp:\n                    dp[i, d] = dp[j, d] + 1\n                else:\n                    dp[i, d] = 2\n        return max(dp.values())\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        s = len(A)\n        dp = {}\n        for i in range(s):\n            for j in range(i+1, s):\n                diff = A[j] - A[i]\n                if (i, diff) in dp:\n                    dp[(j, diff)] = dp[(i, diff)] + 1\n                else:\n                    dp[(j, diff)] = 2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = collections.defaultdict(int)\n        \n        for i in range(0, len(A)):\n            for j in range(i + 1, len(A)):\n                diff = A[j] - A[i]\n                if (i, diff) in dp:\n                    dp[(j, diff)] = dp[(i,diff)] + 1\n                else:\n                    dp[(j,diff)] = 2\n                \n            \n        return max(dp.values())\n                    \n            \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = [0 for _ in range(501)]\n        onleftlen = 0\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lvali in range(onleftlen):\n                lval = onleftl[lvali]\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleftl[onleftlen] = val\n                onleftlen += 1\n                onleft.add(val)\n        return res", "from functools import lru_cache\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        if len(A) <= 2:\n            return len(A)\n        \n        n = len(A)\n        memo = {}\n        \n        for i in range(n):\n            for j in range(i + 1,n):\n                diff = A[j] - A[i]\n                memo[(j, diff)] = memo[(i, diff)] + 1 if (i, diff) in memo else 2\n        \n        return max(memo.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        maximum = 0\n        n=len(A)\n        dp={}\n        for i in range(n):\n            for j in range(i+1,n):\n                dif = A[j]-A[i]\n                if (i,dif) in dp :\n                    dp[(j,dif)]=dp[(i,dif)]+1\n                else:\n                    dp[(j,dif)]=2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range (len(A)):\n            for j in range (i+1,len(A)):\n                d = A[j] - A[i]\n                if (i,d) in dp:\n                    dp[j,d] = dp[i,d]+1\n                else:\n                    dp[j,d] = 2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:  \n        memo = dict()\n        n = len(A)\n        for i in range(n-1,-1,-1):\n            for j in range(n-1,i,-1):\n                d = A[j] - A[i]\n                if (j,d) in memo:\n                    memo[(i,d)] = memo[(j,d)] + 1\n                else:\n                    memo[(i,d)] = 2\n        return max(memo.values())\n", "from heapq import heappop, heapify\n\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        # KEEP TRACK OF THE MAXIMUM COUNT\n        count = 0\n        \n        # dp[i][d] = Length of arithmetic subsequence ending at A[i] (inclusive), with diff = d\n        dp = defaultdict(lambda: defaultdict(int))\n        \n        for i in range(1, len(A)):\n            seen = set()\n            for j in range(i - 1, -1, -1):\n                diff = A[i] - A[j]\n                if diff not in seen:\n                    dp[i][diff] += dp[j][diff] + 1\n                    count = max(count, dp[i][diff])\n                    seen.add(diff)\n                    \n        # for k, v in dp.items():\n        #     print(k, v)\n                                  \n        return count + 1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                gap = A[j] - A[i]\n                if (i, gap) in dp:\n                    dp[(j, gap)] = dp[(i, gap)] + 1\n                else:\n                    dp[(j, gap)] = 2\n                    \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dic = {}\n        for i, val2 in enumerate(A[1:], start = 1):\n            for j, val in enumerate(A[:i]):\n                diff = val2 - val\n                if (j,diff) in dic:\n                    dic[i,diff] = dic[j,diff] + 1\n                else:\n                    dic[i,diff] = 2\n                \n        return max(dic.values())\n", "from itertools import repeat\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = 0\n        onleftl = []\n        onright = [*repeat(0, 501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            b = (1<<val)\n            if not (onleft & b):\n                onleftl.append(val)\n                onleft = (onleft | b)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleft:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        di={}\n        for i in range(1,len(A)):\n            for j in range(i):\n                d=A[i]-A[j]\n                if (j,d) in di:\n                    di[i,d]=di[j,d]+1\n                else:\n                    di[i,d]=2\n        return max(di.values())\n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        n = len(A)\n        \n        # 2 4 6\n        # 2 1 4 6\n        # 2 3 4 6 8\n        \n        increment = 0\n        num_steps = 0\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[j] - A[i]\n                \n                if (i,diff) not in dp:\n                    dp[j,diff] = 2\n                else:\n                    dp[j,diff] = dp[i,diff] + 1\n        \n        return max(dp.values())\n            \n            \n                \n                \n            \n            \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                delta = A[j] - A[i]\n                dp[(j, delta)] = dp[(i, delta)] + 1 if (i, delta) in dp else 2\n        return max(dp.values())", "from collections import OrderedDict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # When adding a new number A[j], we look at all previous numbers A[i]:\n        # (1) If A[j] can extend any arithmetic subsequence currently ends at A[i]: LAS += 1\n        # (2) Otherwise, LAS = 2\n        subseq_lengths = {}\n        for j in range(1, len(A)):\n            for i in range(j):\n                diff = A[j] - A[i]\n                subseq_lengths[diff, j] = subseq_lengths.get((diff, i), 1) + 1\n        return max(subseq_lengths.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if (j, diff) in dp:\n                    dp[(i, diff)] = dp[(j, diff)] + 1\n                else:\n                    dp[(i, diff)] = 2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        if n < 3:\n            return n\n        d = {}\n        for i in range(n - 1):\n            for j in range(i + 1, n):\n                diff = A[j] - A[i]\n                if (i, diff) in d:\n                    d[(j, diff)] = d[(i, diff)] + 1\n                else:\n                    d[(j, diff)] = 2\n        return max(d.values())", "#Memorize: arithmetic sequence has >=2 length.\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        memo = {}\n        for i in range(len(A)):\n            for j in range(i):\n                interval = A[i]-A[j]\n                if (interval, j) in memo:\n                    memo[(interval, i)] = memo[(interval, j)]+1\n                else:\n                    memo[(interval, i)] = 2\n        return max(memo.values())\n", "'''\n9 4 7 2 10\n\n\u62bd\u4e24\u4e2a\u6570 \u5f97\u5230\u7b49\u5dee \u8ba1\u7b97\u6240\u6709\u7684\u7ec4\u5408\ndp[(j, dif)]\u6307\u7684\u662f\u4ee5dif\u4e3a\u7b49\u5dee\uff0c \u622a\u81f3\uff08\u5305\u542b\uff09A[j], \u7684\u6700\u957f\u7b49\u5dee\u5e8f\u5217\n'''\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dif = A[j] - A[i]\n                dp[(j, dif)] = dp.get((i, dif), 1) + 1\n        return max(dp.values())\n\n            \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        mark = [{}]\n        res = 0\n        \n        for i in range(1, n):\n            mark.append({})\n            for j in range(i):\n                delta = A[i] - A[j]\n                if delta in mark[j]:\n                    if delta in mark[i]:\n                        mark[i][delta] = max(mark[j][delta] + 1, mark[i][delta])\n                    else:\n                        mark[i][delta] = mark[j][delta] + 1\n                else:\n                    if delta not in mark[i]:\n                        mark[i][delta] = 1\n                    \n                if mark[i][delta] > res:\n                    res = mark[i][delta]\n                    \n        return res+1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                d = A[j] - A[i]\n                dp[j, d] = dp.get((i, d), 1) + 1\n        return max(dp.values())", "from array import array\nfrom itertools import repeat\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = 0\n        onleftl = []\n        onright = array('H', repeat(0, 501))\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            b = (1<<val)\n            if not (onleft & b):\n                onleftl.append(val)\n                onleft = (onleft | b)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = dict()\n        n = len(A)\n        for i in range(n):\n            for j in range(i+1,n):\n                delta = A[j]-A[i]\n                dp[(j, delta)] = dp.get((i, delta), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [[1] * 1001 for _ in range(501)]\n        res = 2\n        for i, a in enumerate(A):\n            flag = True\n            for j in range(i):\n                d = a - A[j]\n                if d == 0 and not flag: continue\n                dp[a][d] = dp[A[j]][d] + 1\n                res = max(res, dp[a][d])\n                if d == 0: flag = False\n                # if res == dp[a][d]: print(a, d, dp[a][d])\n        return res\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {} \n        for i in range(len(A)): \n            for j in range(i+1, len(A)): \n                delta = A[j]-A[i] \n                if (delta, i) in dp:  \n                    dp[(delta, j)] = dp[(delta, i)] + 1 \n                else: \n                    dp[(delta, j)] = 2\n                 \n        return max(dp.values()) \n                \n         \n         \n         \n            \n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        DP={}\n        for i in range(1,len(A)):\n            for j in range(len(A[:i])):\n                d=A[i]-A[j]\n                if (j,d) in DP: DP[i,d]=DP[j,d]+1\n                else: DP[i,d]=2\n        return max(DP.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}; n = len(A)\n        for i in range(n):\n            for j in range(i+1, n):\n                b = A[j] - A[i]\n                if (i,b) not in dp: dp[j,b] = 2\n                else              : dp[j,b] = dp[i,b] + 1\n        return max(dp.values())\n", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = defaultdict(int)\n        maxVal = 2\n        for i in range(1,len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                # print(d)\n                if (j,diff) not in d:\n                    d[(i,diff)] = 2\n                else:\n                    # if (j,diff) in d:\n                    d[(i,diff)] = d[(j,diff)] + 1 \n                    \n                    \n                        \n        # print(d)\n        return max(d.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        #dictionary of pairs (i pointer, dist)\n        \n        # i pointer iterates from 1index to the end\n        for i in range(1, len(A)):\n            # j pointer iterates from 0 to just left of i pointer then resets\n            for j in range(0, len(A[:i])):\n                \n                #finds the difference of the two values\n                d = A[i] - A[j]\n                \n                #checks to see if the same diff exists at j\n                if (j, d) in dp:\n                    #if j,d is in dp then add 1 because the value at i has the same difference and set that as i,d\n                    dp[i, d] = dp[j, d] + 1\n                #if not then its set to two because that accounts for the i,j as two integers\n                else:\n                    dp[i, d] = 2\n                    \n        #return what ever is the highest value of all the keys in the dictionary is\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleft:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                c = tex.get(diff, 2) + 1\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                diff = A[j] - A[i]\n                dp[j, diff] = dp.get((i, diff), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        mapp=collections.defaultdict(int)\n        if not A:\n            return -1\n        \n        for i in range(1,len(A)):\n            for j in range(0,len(A[:i])):\n                d=A[i]-A[j]\n                if (d,j) in mapp:\n                    mapp[(d,i)]=mapp[d,j]+1\n                else:\n                    mapp[d,i]=2\n        return max(mapp.values())\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n          n=len(A);dp={};s=set(A)\n          for i in range(1,n):\n               for j in range(i):\n                    d=A[i]-A[j]\n                    if j!=0 and (j,d) in dp:\n                        dp[(i,d)]=1+dp[(j,d)]\n                    else:\n                        dp[(i,d)]=2\n          l=dp.values()\n          return max(l)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[(idx, diff)]: length of arithmetic sequence at index with difference diff.\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                diff = A[j] - A[i]\n                dp[(j, diff)] = dp.get((i, diff), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        diff = {}\n        for i in range(n):\n            for j in range(i+1, n):\n                d = A[j] - A[i]\n                if (i, d) in diff:\n                    diff[(j, d)] = diff[(i, d)] + 1\n                else:\n                    diff[(j, d)] = 2\n        return max(diff.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) <= 1:\n            return len(A)\n        \n        memo = [(1 + 2 * 500) * [1] for _ in range(1 + len(A))]\n        \n        res = 2\n        \n        for i in range(len(A)-2, -1, -1):\n            Ai = A[i] - 500\n            mi = memo[i]\n            \n            for j in range(i+1, len(A)):\n                diff = A[j] - Ai\n                mi[diff] = max(mi[diff], memo[j][diff] + 1)\n        \n            res = max(res, max(mi))\n        \n        return res\n        \n#         h = dict()\n        \n#         res = 2\n        \n#         for i, ai in enumerate(A):\n#             for j in range(i):\n#                 diff = A[j] - ai\n#                 aux = (j, diff)\n#                 if aux in h:\n#                     h[(i, diff)] = h[aux] + 1\n#                     # res = max(res, h[(i, diff)])\n#                 else:\n#                     h[(i, diff)] = 2\n    \n#         return max(h.values())\n", "from itertools import repeat\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = 0\n        onleftl = []\n        onright = {}\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            if v not in onright:\n                onright[v] = 1\n            else:\n                onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval not in onright or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            b = (1<<val)\n            if not (onleft & b):\n                onleftl.append(val)\n                onleft = (onleft | b)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                b = A[j] - A[i]\n                if (i,b) not in dp: \n                    dp[j,b] = 2\n                else: \n                    dp[j,b] = dp[i,b] + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        diffdict = {}\n        max = 0\n        \n        for i in range(len(A)):\n            \n            for j in range(i):\n                \n                diff = A[i]-A[j]\n                if diff not in diffdict:\n                    diffdict[diff] = {i: 2}\n                else:\n                    if j in diffdict[diff]: \n                        diffdict[diff][i] = diffdict[diff][j] + 1\n                    else:\n                        diffdict[diff][i] = 2\n                if diffdict[diff][i] > max:\n                        max = diffdict[diff][i]\n                    \n        return  max\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 0\n        dic = {}\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                d = A[j]-A[i]\n                dic[j,d]=dic.get((i,d),1)+1\n                # if (i,d) in dic:\n                #     dic[(j,d)]=dic[(i,d)]+1\n                # else:\n                #     dic[(j,d)]=2\n                \n        return max(dic.values())\n", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = collections.defaultdict(int)\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                j_key = (j, diff)\n                i_key = (i, diff)\n                if j_key in dp:\n                    dp[i_key] = dp[j_key] + 1\n                else:\n                    dp[i_key] = 2\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i, a2 in enumerate(A[1:], start=1):\n            for j, a1 in enumerate(A[:i]):\n                d = a2 - a1\n                if (j, d) in dp:\n                    dp[i, d] = dp[j, d] + 1\n                else:\n                    dp[i, d] = 2\n        return max(dp.values())\n    \n    \n        f = collections.defaultdict(int)\n        maxlen = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                #fff[(A[i], diff)] = max(fff[(A[i], diff)], fff.get((A[j], diff), 1) + 1)\n                f[(i, diff)] = max(f[(i, diff)], f.get((j, diff), 1) + 1)\n                '''\n                if (j, diff) not in f:\n                    f[(i, diff)] = 2\n                else:\n                    f[(i, diff)] = max(f[(i, diff)],  f[(j, diff)] + 1)                \n                '''                    \n                maxlen = max(maxlen, f[(i, diff)])\n\n        return maxlen\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        mem = [[0]*1001 for i in range(len(A))]\n        ret = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                mem[i][diff] = mem[j][diff] + 1\n                ret = max(ret, mem[i][diff])\n        return ret+1", "'''\nsubseq: non contiguous\n\nB[i+1] - B[i] is always the same \nfor each we need the last elt in seq \n\ngoal: return the length of longest \n\nstate:\ni, last in seq, diff, length of the seq \n\nhelper(i, last, diff, l)\n\nchoices:\n - use (condition A[i] - last == diff)\n - skip \n - start looking for a new arith seq ? \n\nuse: \nif A[i] - last == diff:\n    helper(i + 1, A[i], diff, l + 1)\n    \nhelper(i+1, last, diff, l)\n\nif i > 0:\n    helper(i + 1, A[i], A[i] - A[i - 1], 2)\n   \ndp[(i, diff)]: length of longest arith seq ending at i with difference diff \n\n\n'''\nfrom functools import lru_cache\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = {}\n        \n        for i in range(n):\n            for j in range(i + 1, n):\n                d = A[j] - A[i]\n                dp[(j, d)] = dp.get((i, d), 1) + 1\n        return max(dp.values())\n           \n                \n                \n                \n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i in range(len(A)):\n            for j in range(i):\n                d = A[j] - A[i]\n                dp[i, d] = dp.get((j, d), 0) + 1\n                    \n        return max(dp.values()) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i in range(len(A)):\n            samecnt = 1\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff == 0:\n                    samecnt += 1\n                else:\n                    dp[A[i], diff] = dp.get((A[j], diff), 1) + 1\n            dp[A[i], 0] = samecnt\n        \n        # print(dp)\n        key = max(dp, key=dp.get)\n        return dp[key]", "from itertools import repeat\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = []\n        onright = list(repeat(0, 501))\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                c = tex.get(diff, 2) + 1\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleftl.append(val)\n                onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(1, len(A)):\n            for j in range(i):\n                num1 = A[j]\n                num2 = A[i]\n                d = num2 - num1\n                if (j, d) in dp:\n                    dp[(i, d)] = dp[(j, d)] + 1\n                else:\n                    dp[(i, d)] = 2\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        L = len(A)\n        for i in range(L):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[(i, diff)] = dp.get((j, diff), 1) + 1\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # When adding a new number A[j], we look at all previous numbers A[i]:\n        # (1) If A[j] can extend any arithmetic subsequence currently ends at A[i]: LAS += 1\n        # (2) Otherwise, LAS = 2\n        subseq_lengths = {}\n        for j in range(1, len(A)):\n            for i in range(j):\n                diff = A[j] - A[i]\n                if (diff, i) in subseq_lengths:\n                    subseq_lengths[diff, j] = subseq_lengths[diff, i] + 1\n                else:\n                    subseq_lengths[diff, j] = 2\n        return max(subseq_lengths.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i, a in enumerate(A[1:], 1):\n            for j, b in enumerate(A[:i]):\n                d = a - b\n                dp[i, d] = dp.get((j, d), 0) + 1\n                    \n        return max(dp.values()) + 1", "class Solution:\n    def longestArithSeqLength(self, nums: List[int]) -> int:\n        dp=dict()\n        for i in range(len(nums)):\n            for j in range(i+1,len(nums)):\n                diff=nums[j]-nums[i]\n                dp[(j,diff)]=dp.get((i,diff),1)+1\n\n        # print(dp)\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        memo = {}\n        \n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[j] - A[i]\n                memo[j, diff] = memo.get((i, diff), 1) + 1\n                \n        return max(memo.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp, n = {}, len(A)\n        for i in range(n):\n            for j in range(i+1, n):\n                diff = A[j] - A[i]\n                dp[(j, diff)] = dp.get((i, diff), 1) + 1\n                \n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i in range(len(A)):\n            samecnt = 1\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff == 0:\n                    samecnt += 1\n                else:\n                    dp[A[i], diff] = dp.get((A[j], diff), 1) + 1\n            dp[A[i], 0] = samecnt\n        \n        # print(dp)\n        key = max(dp, key=dp.get)\n        # print(key)\n        return dp[key]", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # O(N ** 2) DP\n        dp = {}\n        n = len(A)\n        for i in range(n):\n            for j in range(i + 1, n):\n                diff = A[j] - A[i]\n                dp[(j, diff)] = dp.get((i, diff), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[index][diff] := the longest arithmetic subsequence in A[:index+1] with difference = diff\n        dp = collections.defaultdict(int)\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[j] - A[i]\n                dp[(j, diff)]  = dp.get((i,diff), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        '''\n        Maintain a dictionary of differences at each position.\n        The keys are going to be (position, diff)\n        \n        Compare each value with all the values after it\n        calculate the diff and store in the dictionary using the equation in the solution.\n        This equation is the crux of the solution.\n        \n        Do d dry run with example for better understanding\n        https://leetcode.com/problems/longest-arithmetic-subsequence/discuss/274611/JavaC%2B%2BPython-DP\n        '''\n        if not A:\n            return 0\n        \n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[j] - A[i]\n                dp[j, diff] = dp.get((i, diff), 1) + 1\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        \n        dp = [None] * len(A)\n        dp[0] = {0:1}\n        \n        max_val = 1\n        \n        for i in range(1, len(A)):\n            dp[i] = {}\n                        \n            for j in range(i):\n                diff = A[i] - A[j]\n                diff_val = 2\n                \n                if diff in dp[j]:\n                    diff_val = dp[j][diff] + 1\n                \n                dp[i][diff] = diff_val\n\n                max_val = max(max_val, diff_val)\n                    \n        return max_val", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i in range(len(A)):\n            for j in range(i):\n                step = A[i]-A[j]\n                dp[(i,step)] = dp.get((j,step),1)+1\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        diffs = set(y - x for i, x in enumerate(A) for y in A[i+1:])\n        ans = 0\n        for diff in diffs:\n            data = {}\n            for num in A:\n                if num - diff not in data:\n                    if num not in data:\n                        data[num] = [num]\n                    continue\n                if len(data[num - diff]) < len(data.get(num, [])):\n                    continue\n                seq = data.pop(num - diff)\n                seq.append(num)\n                ans = max(ans, len(seq))\n                data[num] = seq\n        return ans", "import numpy as np\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onright = np.zeros(501, dtype=np.int16)\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleft:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [{} for _ in range(len(A))]\n        # maxSequence = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i][diff] = dp[j].get(diff,1) + 1\n                # if val > maxSequence:\n                #     maxSequence = val\n        return max(v1 for dic in dp for v1 in dic.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) <= 2:\n            return len(A)\n        n, ans = len(A), 0\n        dp = [[0] * 1001\n              for _ in range(n)]\n        for j in range(n):\n            for i in range(0, j):\n                diff = A[j] - A[i] + 500\n                dp[j][diff] = dp[i][diff] + 1\n                ans = max(ans, dp[j][diff])\n        \n        return ans + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = []\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleft:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                c = tex.get(diff, 2) + 1\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleftl.append(val)\n                onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        \n        dp = [{0: 1}]\n        \n        max_val = 1\n        \n        for i in range(1, len(A)):\n            dp.append({0: 1})\n                        \n            for j in range(i):\n                diff = A[i] - A[j]\n                diff_val = 2\n                \n                if diff in dp[j]:\n                    diff_val = dp[j][diff] + 1\n                \n                dp[-1][diff] = diff_val\n\n                max_val = max(max_val, diff_val)\n                    \n        return max_val", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # table[index][diff] equals to the length of \n        # arithmetic sequence at index with difference diff.\n        table = dict()\n        max_v = 0\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                \n                _diff = A[j] - A[i]\n                if (i,_diff) in table.keys():\n                    table[j,_diff] = table[i,_diff] + 1\n                else:\n                    table[j,_diff] = 2 # the first diff\n                    # will corrspond to two values [v1,v2]\n                #max_v = max(max_v,table[j,_diff])\n                    \n        return max(table.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        n = len(A)\n        if n < 3: return n\n        \n        dp = [{} for _ in range(n)]\n        max_ = 0\n        for i in range(1, n):\n            for j in range(i):\n                \n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                \n                max_ = max(max_, dp[i][diff])\n        \n        return max_\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n          n=len(A);dp={};s=set(A)\n          for i in range(1,n):\n               for j in range(i):\n                    d=A[i]-A[j]\n                    if (j,d) in dp:\n                        dp[(i,d)]=1+dp[(j,d)]\n                    else:\n                        dp[(i,d)]=2\n          l=dp.values()\n          return max(l)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        diffs = [collections.defaultdict(int) for _ in range(len(A))]\n        diffs[0][0] = 1\n        max_len = 1\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in diffs[j]:\n                    diffs[i][diff] = diffs[j][diff] + 1\n                else:\n                    diffs[i][diff] = 2\n                \n                max_len = max(max_len, diffs[i][diff])\n        \n        \n        return max_len", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        table = dict()\n        \n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                \n                _diff = A[j] - A[i]\n                if (i,_diff) in table.keys():\n                    table[j,_diff] = table[i,_diff] + 1\n                else:\n                    table[j,_diff] = 2\n                    \n        return max(table.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [{} for _ in range(len(A))]\n        for i in range(len(A) - 1):\n            for j in range(i + 1, len(A)):\n                diff = A[j] - A[i]\n                diffMap = dp[i]\n                dp[j][diff] = diffMap.get(diff, 1) + 1\n\n        maxLength = 0\n\n        for i in range(len(A)):\n            for diff in dp[i]:\n                if dp[i][diff] > maxLength: maxLength = dp[i][diff]\n\n        return maxLength        \n# O(n2) time, O(n2) space   \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        #onleftl = []\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleft:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                #onleftl.append(val)\n                onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j]-A[i]] = dp.get((i, A[j]-A[i]), 1) + 1\n        \n        return max(dp.values())\n                \n                    \n                \n                \n", "#from collections import Counter\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A)<=2:\n            return len(A)\n        dp = [{} for _ in range(len(A))]\n        ans = 2\n        for i in range(len(A)):\n            for j in range(i+1,len(A),1):\n                diff = A[j]-A[i]\n                if diff in dp[i]:\n                    dp[j][diff] = dp[i][diff] + 1\n                else:\n                    dp[j][diff] = 2\n                ans = max(ans, dp[j][diff])\n        return ans\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        ans = 2\n        \n        if n < 2:\n            return n\n        h = max(A)\n        dp = [ {} for j in range(n)]\n        # good idea, we did not know d, and we use dict here\n        ans = 0\n        for i in range(n):\n            for j in range(i):\n                d = A[i] - A[j]\n                if d in dp[j]:\n                    \n                    dp[i][d] =  dp[j][d] + 1\n                else:\n                    dp[i][d] = 2\n                ans = max(ans, dp[i][d])\n        \n        return ans\n\n", "class Solution:\n    def longestArithSeqLength(self, A):\n        n = len(A)\n        dp = [{} for _ in range(n)]\n        res = 0\n        for j in range(n):\n            for i in range(j):\n                d = A[j] - A[i]\n                if d in dp[i]:\n                    dp[j][d] = dp[i][d] + 1\n                else:\n                    dp[j][d] = 2\n                res = max(res, dp[j][d])\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i,A[j] - A[i]) , 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        if not A:\n            return 0\n        \n        n = len(A)\n        dp = dict()\n        \n        for r in range(1, n):\n            for l in range(r):\n                # pr\n                dp[r, A[r] - A[l]] = dp.get((l, A[r] - A[l]), 1) + 1\n                \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        for i in range(len(A)):\n            dp.append({})\n             \n        # print(dp)\n        res = 2\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                res = max(res,dp[i][diff])\n                \n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        table = dict()\n        \n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                \n                _diff = A[j] - A[i]\n                if (i,_diff) in list(table.keys()):\n                    table[j,_diff] = table[i,_diff] + 1\n                else:\n                    table[j,_diff] = 2\n                    \n        return max(table.values())\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        res = 0\n        dp = [{} for _ in range(len(A))]\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                res = max(res, dp[i][diff])\n                \n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dict1 = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dict1[(A[j] - A[i], j)] = dict1.get((A[j] - A[i], i), 1) + 1\n        return max(dict1.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i):\n                dp[(i, A[i] - A[j])] = dp.get((j, A[i] - A[j]), 1) + 1\n\n        return max(dp.values())        ", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n                \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n#         Explanation\n#         dp[index][diff] equals to the length of arithmetic sequence at index with difference diff.\n\n#         Complexity\n#         Time O(N^2)\n#         Space O(N^2)\n        \n        n = len(A)\n        if n< 2:\n            return n\n        dp = {}\n        for i in range(n):\n            for j in range(i+1, n):\n                dp[(j, A[j]-A[i])] = dp.get((i, A[j]-A[i]), 1) + 1 # if there is no such key, len is 1 ==> A[i]\n        return max(dp.values())\n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i):\n                dp[(i, A[i] - A[j])] = dp.get((j, A[i] - A[j]), 1) + 1\n        return max(dp.values())", "class Solution:\n\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        n=len(A)\n        DP={}\n        for i in range(1,len(A)):\n            for j in range(0,i):\n                if (j, A[i]-A[j]) in DP:\n                    DP[(i,A[i]-A[j])]= DP[(j, A[i]-A[j])]+1\n                else:\n                    DP[(i,A[i]-A[j])]=2\n                \n        return max(DP.values())\n                \n        \n        n=len(A)\n        dp={}\n        for i in range(n):\n            for j in range(i+1,n):\n                dif = A[j]-A[i]\n                if (i,dif) in dp :\n                    dp[(j,dif)]=dp[(i,dif)]+1\n                else:\n                    dp[(j,dif)]=2\n        return max(dp.values())", "from itertools import repeat\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = 0\n        onleftl = []\n        onright = [*repeat(0, 501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                c = tex.get(diff, 2) + 1\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            b = (1<<val)\n            if not (onleft & b):\n                onleftl.append(val)\n                onleft = (onleft | b)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[index][diff] equals to the length of arithmetic sequence at index with difference diff.\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j]-A[i]] = dp.get((i, A[j]-A[i]), 1) + 1\n        return max(dp.values())\n        \n            \n            \n", "#[Runtime: 4284 ms, faster than 10.74%] DP\n#O(N^2)\n#NOTE: diff can be either positive or negative\n#f[i]: the longest length of arithmetic subsequences who takes A[i] as the tail.\n#f[i] = defaultdict(lambda: 1)\n#f[i] = {diff: longest length}\n#f[i] = max(f[i][d], f[j][d] += 1) for j < i and d:=A[i]-A[j]\nfrom collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [defaultdict(lambda: 1) for _ in range(len(A))]\n        for i, a in enumerate(A):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if dp[i][diff] < dp[j][diff] + 1: \n                    dp[i][diff] = dp[j][diff] + 1\n        return max(max(lens.values()) for lens in dp)", "# from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(int)\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[(i,diff)] = dp[(j,diff)] + 1\n        # print(dp)\n        return max(dp.values()) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n#         n = len(A);\n#         res = 1;\n        \n#         for i in range(1,n):\n#             for j in range(i):\n#                 count = 2; \n#                 x = i + 1;\n#                 y = i;\n                \n#                 while x < n and y < n:\n#                     if A[x] - A[y] == A[i] - A[j]:\n#                         count += 1;\n#                         y = x\n#                     x += 1;\n#                 res = max(res, count);\n        \n#         return res;\n\n        \n        dp = dict();\n        n = len(A);\n        for i in range(n):\n            for j in range(i+1,n):\n                dp[(j, A[j] - A[i])] = dp.get((i, A[j] - A[i]), 1) + 1;\n        \n        return max(dp.values());\n                \n        \n                        \n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n\n        d = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                d[j, A[j] - A[i]] = d.get((i, A[j] - A[i]), 1) + 1\n        return max(d.values())\n", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[(j, A[j]-A[i])] = dp.get((i, A[j]-A[i]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp={}\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                \n                dp[j,A[j]-A[i]]=dp.get((i,A[j]-A[i]),1)+1\n        \n        return max(dp.values())", "from itertools import repeat\nfrom collections import Counter\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = []\n        onright = dict(Counter(A))\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if onright.get(nextval, 0) == 0:\n                    continue\n                c = tex.get(diff, 2) + 1\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleftl.append(val)\n                onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #9,4,7,2,10\n        #0,5,2,7,-1\n        N=len(A)\n        dp={}\n        for i in range(N):\n            for j in range(i+1,N):\n                dp[j,A[j]-A[i]]=dp.get((i,A[j]-A[i]),1)+1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        ret = 0\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j]-A[i]] = dp.get((i, A[j]-A[i]), 1) + 1\n\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) < 2: return len(A)\n        dp = [{} for _ in range(len(A))]\n        res = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j] \n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                res = max(res, dp[i][diff])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j]-A[i]] = dp.get((i, A[j]-A[i]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n                \n        #print(dp)\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, nums: List[int]) -> int:\n        n = len(nums)\n        if n <= 2:\n            return n\n        dp = dict()\n        for i in range(n):\n            for j in range(i + 1, n):\n                d = nums[j] - nums[i]\n                if (i, d) in list(dp.keys()):\n                    dp[(j, d)] = dp[(i, d)] + 1\n                else:\n                    dp[(j, d)] = 2\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res, d = 0, [{} for _ in range(len(A))]\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in d[j]:\n                    d[i][diff] = d[j][diff] + 1\n                else:\n                    d[i][diff] = 2\n                res = max(res, d[i][diff])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        dp = [[2] * N for i in range(N)]\n        ans = 0\n        for i in range(N):\n            pos = {}\n            for j in range(i):\n                x = 2*A[j] - A[i]\n                if x in pos:\n                    dp[i][j] = max(dp[i][j], 1 + dp[j][pos[x]])\n                ans = max(ans, dp[i][j])\n                pos[A[j]] = j\n        \n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[(j, A[j]-A[i])] = dp.get((i, A[j]-A[i]), 1) + 1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #446. Arithmetic Slices II - Subsequence\n        '''\n        https://leetcode.com/problems/longest-arithmetic-sequence/discuss/274611/JavaC++Python-DP\n        dp[diff][index] + 1 equals to the length of arithmetic sequence at index with difference diff.\n        '''\n        '''\n        Input: [3,6,9,12]\nOutput: 4\nExplanation: \nThe whole array is an arithmetic sequence with steps of length = 3.\nInput: [9,4,7,2,10]\nOutput: 3\nExplanation: \nThe longest arithmetic subsequence is [4,7,10].\nInput: [20,1,15,3,10,5,8]\nOutput: 4\nExplanation: \nThe longest arithmetic subsequence is [20,15,10,5].\n        '''\n        '''\nInput: [3,6,9,12]\nOutput: 4\nExplanation: \nThe whole array is an arithmetic sequence with steps of length = 3.\n        defaultdict(<class 'int'>, {})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1, (9, 0): 0, (9, 3): 1})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1, (9, 0): 0, (9, 3): 1, (3, 2): 2})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1, (9, 0): 0, (9, 3): 1, (3, 2): 2, (6, 1): 0, (6, 3): 1})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1, (9, 0): 0, (9, 3): 1, (3, 2): 2, (6, 1): 0, (6, 3): 1, (3, 3): 3})\n      ''' \n        #longest arithmetic subseq\n    \n        '''\n        Input:\n[24,13,1,100,0,94,3,0,3]\nOutput:\n3\nExpected:\n2\n        '''\n        '''\n        Input:\n[0,8,45,88,48,68,28,55,17,24]\nOutput:\n4\nExpected:\n2\n        '''\n      \n  \n        \n        '''Len = len(A)\n        res = 0\n        for i in range(aLen):\n            for j in range(i+1, aLen):\n                diff = A[j]-A[i]\n                target = A[j] + diff \n                count = 2 \n                idx = j+1\n                while idx < aLen:\n                    if A[idx] == target:\n                        count += 1\n                        target = target + diff \n                    idx += 1\n                res = max(res, count)\n        return res'''\n        \n       \n                \n        '''aLen =len(A)\n        res = 0 \n        dp = [{} for _ in range(aLen)]\n        for i in range(aLen):\n            for j in range(i):\n                diff = A[i]-A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = 1 + dp[j][diff]\n                else:\n                    dp[i][diff] = 2\n                res = max(res, dp[i][diff])\n        return res '''\n        \n        '''aLen = len(A)\n        res = 0 \n        dp = [{} for _ in range(aLen)]\n        for i in range(aLen):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = 1+ dp[j][diff]\n                else:\n                    dp[i][diff] = 2 \n                res = max(res, dp[i][diff])\n        return res '''\n        \n        aLen = len(A)\n        res = 0 \n        dp = [{} for _ in range(aLen)]\n        for i in range(aLen):\n            for j in range(i):\n                diff = A[i]-A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2 \n                res = max(res, dp[i][diff])\n        return res \n                    \n            \n \n\n                    \n                    \n \n\n        \n        '''\n        #why is this solution the fastest??????\n        nums_map = {}\n        for i,n in enumerate(A):\n            nums_map[n] = nums_map.get(n, [])\n            nums_map[n].append(i)\n        max_length = 2\n        for i, n in enumerate(A):\n            for j in range(i+1, len(A)):\n                m = A[j]\n                target = m + (m-n)\n                length = 2\n                last_index = j\n                found = True\n                while target in nums_map and found:\n                    found = False\n                    for index in nums_map[target]:\n                        if index > last_index:\n                            last_index = index\n                            length += 1\n                            target += m-n\n                            max_length = max(max_length, length)\n                            found = True\n                            break\n                    if not found:\n                        break\n        return max_length'''\n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[index][diff]  equals to the length of arithmetic sequence at index with difference diff.\n        # O(n^2)\n        # A is unsorted \n        \n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1  # A is unsorted, so it is dp.get((i, A[j]-A[i])) not dp.get(j-1, diff)\n        \n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        ##pretty sure this soln works, it just takes too long.\n#         def searchList(num: int, lst: List[int]) -> bool:\n#             for curr in lst:\n#                 if curr == num:\n#                     return True\n#             return False\n        \n#         longest = 2 #shorest array we're given is 2        \n#         info = []\n#         subseq = {\n#             'diff': A[1] - A[0],\n#             'seq': A[0:2]\n#         }\n#         info.append(subseq)\n        \n        \n#         for i in range(2, len(A)):\n#             seen = []\n#             curr = A[i]\n#             prev = A[i - 1]\n#             for sub in info:\n#                 if curr - sub['seq'][-1] == sub['diff']:\n#                     sub['seq'].append(curr)\n#                     seen.append(sub['seq'][-1])\n#                     if len(sub['seq']) > longest:\n#                         longest = len(sub['seq'])            \n#             for num in A[0:i]:\n#                 #if an element hasn't been seen, append another info subseq dict\n#                 #with the current element and the unseen one\n#                 if not searchList(num, seen):\n#                     diff = curr - num\n#                     #if curr + diff < 0, then we know that the subseq will not continue, so don't\n#                     #bother keeping it\n#                     if curr + diff >= 0:\n#                         info.append({\n#                             'diff': curr - num,\n#                             'seq': [num, curr]\n#                         })\n#         return longest\n\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # 9 ,4    ,     7,         10\n        #    ^\n#             (1,-5): 2  (2, 3): 2   (3, 3):3\n#                                    (3, -5):3\n        \n        dict = collections.defaultdict(lambda: 0)\n        \n        for i in range(len(A) - 1):    \n            for j in range(i + 1, len(A)):\n                dict[(j, A[j] - A[i])] = dict.get((i, A[j] - A[i]), 1) + 1\n        \n        return max(dict.values())\n        \n", "\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[(j, A[j] - A[i])] = dp.get((i, A[j] - A[i]), 1) + 1 \n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [{} for i in range(len(A))]\n        longest = 0\n\n        for i in range(len(A)):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                longest = max(longest, dp[i][diff])\n\n        return longest", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #446. Arithmetic Slices II - Subsequence\n        '''\n        https://leetcode.com/problems/longest-arithmetic-sequence/discuss/274611/JavaC++Python-DP\n        dp[diff][index] + 1 equals to the length of arithmetic sequence at index with difference diff.\n        '''\n        '''\n        Input: [3,6,9,12]\nOutput: 4\nExplanation: \nThe whole array is an arithmetic sequence with steps of length = 3.\nInput: [9,4,7,2,10]\nOutput: 3\nExplanation: \nThe longest arithmetic subsequence is [4,7,10].\nInput: [20,1,15,3,10,5,8]\nOutput: 4\nExplanation: \nThe longest arithmetic subsequence is [20,15,10,5].\n        '''\n        '''\nInput: [3,6,9,12]\nOutput: 4\nExplanation: \nThe whole array is an arithmetic sequence with steps of length = 3.\n        defaultdict(<class 'int'>, {})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1, (9, 0): 0, (9, 3): 1})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1, (9, 0): 0, (9, 3): 1, (3, 2): 2})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1, (9, 0): 0, (9, 3): 1, (3, 2): 2, (6, 1): 0, (6, 3): 1})\ndefaultdict(<class 'int'>, {(3, 0): 0, (3, 1): 1, (6, 0): 0, (6, 2): 1, (9, 0): 0, (9, 3): 1, (3, 2): 2, (6, 1): 0, (6, 3): 1, (3, 3): 3})\n      ''' \n        #longest arithmetic subseq\n    \n        '''\n        Input:\n[24,13,1,100,0,94,3,0,3]\nOutput:\n3\nExpected:\n2\n        '''\n        '''\n        Input:\n[0,8,45,88,48,68,28,55,17,24]\nOutput:\n4\nExpected:\n2\n        '''\n      \n  \n        \n        '''Len = len(A)\n        res = 0\n        for i in range(aLen):\n            for j in range(i+1, aLen):\n                diff = A[j]-A[i]\n                target = A[j] + diff \n                count = 2 \n                idx = j+1\n                while idx < aLen:\n                    if A[idx] == target:\n                        count += 1\n                        target = target + diff \n                    idx += 1\n                res = max(res, count)\n        return res'''\n        \n       \n                \n        '''aLen =len(A)\n        res = 0 \n        dp = [{} for _ in range(aLen)]\n        for i in range(aLen):\n            for j in range(i):\n                diff = A[i]-A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = 1 + dp[j][diff]\n                else:\n                    dp[i][diff] = 2\n                res = max(res, dp[i][diff])\n        return res '''\n        \n        '''aLen = len(A)\n        res = 0 \n        dp = [{} for _ in range(aLen)]\n        for i in range(aLen):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = 1+ dp[j][diff]\n                else:\n                    dp[i][diff] = 2 \n                res = max(res, dp[i][diff])\n        return res '''\n        \n        aLen = len(A)\n        dp = [{} for _ in range(aLen)]\n        res = 0 \n        for i in range(aLen):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2 \n                res= max(res, dp[i][diff])\n        return res \n                    \n            \n \n\n                    \n                    \n \n\n        \n        '''\n        #why is this solution the fastest??????\n        nums_map = {}\n        for i,n in enumerate(A):\n            nums_map[n] = nums_map.get(n, [])\n            nums_map[n].append(i)\n        max_length = 2\n        for i, n in enumerate(A):\n            for j in range(i+1, len(A)):\n                m = A[j]\n                target = m + (m-n)\n                length = 2\n                last_index = j\n                found = True\n                while target in nums_map and found:\n                    found = False\n                    for index in nums_map[target]:\n                        if index > last_index:\n                            last_index = index\n                            length += 1\n                            target += m-n\n                            max_length = max(max_length, length)\n                            found = True\n                            break\n                    if not found:\n                        break\n        return max_length'''\n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        n = len(A)\n        for i in range(n):\n            for j in range(i + 1, n):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[(j, A[j] - A[i])] = dp.get((i, A[j] - A[i]), 1) + 1\n\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        stored = [{} for i in range(len(A))]\n        best = 0\n        for index, value in enumerate(A):\n            if index == 0:\n                continue\n            for compare in range(index):\n                difference = value - A[compare]\n                stored[index][difference] = 1 + stored[compare].get(difference, 1)\n                best = max(best, stored[index][difference])\n        return best", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        #dictionary of pairs (i pointer, dist)\n        \n        # i pointer iterates from 1index to the end\n        for i in range(1, len(A)):\n            # j pointer iterates from 0 to just left of i pointer then resets\n            for j in range(0, len(A[:i])):\n                \n                #finds the difference of the two values\n                d = A[i] - A[j]\n                \n                #checks to see if the same diff exists at j\n                if (j, d) in dp:\n                    #if j,d is in dp then add 1 because the value at i has the same difference and set that as i,d\n                    dp[i, d] = dp[j, d] + 1\n                #if not then its set to two because that accounts for the i,j as two integers\n                else:\n                    dp[i, d] = 2\n                    \n        #return what ever is the highest value of all the keys in the dictionary is\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[(index, diff)] equals to the length of arithmetic sequence at index with difference diff.\n        dp = collections.defaultdict(int)\n        \n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[(j, A[j] - A[i])] = dp[(i, A[j] - A[i])] + 1\n        \n        return max(dp.values()) + 1", "class Solution:\n    def longestArithSeqLength(self, A):\n        n = len(A)\n        dp = [{} for _ in range(n)]\n        res = 0\n        for i in range(n):\n            for j in range(i + 1, n):\n                d = A[j] - A[i]\n                if d in dp[i]:\n                    dp[j][d] = dp[i][d] + 1\n                else:\n                    dp[j][d] = 2\n                res = max(res, dp[j][d])\n        return res", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())\n#     def longestArithSeqLength(self, A: List[int]) -> int:\n#         m = {}\n        \n#         m[A[1] + A[1]-A[0]] = (2,A[1]-A[0])\n#         for i in range(2, len(A)):\n#             if A[i] in m:\n#                 counter, d = m[A[i]]\n#                 del m[A[i]]\n#                 m[A[i]+d] = (counter+1, d)\n#             else:\n#                 for j in range(0, i):\n#                     d = A[i]-A[j]\n#                     m[A[i]+d] = (2,d)\n#         # print(m)\n#         return max([counter for counter,_ in list(m.values())])\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n , dp = len(A) , {}\n        for i in range(n):\n            for j in range(i + 1 , n):\n                dp[j , A[j] - A[i]] = dp.get((i , A[j] - A[i]) , 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [dict() for _ in range(len(A))]\n        res = 2\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i][diff] = dp[j].get(diff, 1) + 1\n                res = max(dp[i][diff], res)\n        \n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        \n        for i, x in enumerate(A):\n            Dict = collections.defaultdict(int)\n            dp.append(Dict)\n            for j in range(i):\n                diff = x - A[j]\n                dp[i][diff] = dp[j][diff] + 1\n        \n        return max(max(y.values()) for y in dp) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [collections.defaultdict(int) for _ in range(n)] \n        res = 0\n        for i in range(1, n):\n            for j in range(0, i):\n                diff = A[i]-A[j]\n                dp[i][diff] = dp[j][diff] + 1\n                    \n                res = max(res, dp[i][diff])\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A) \n        dp = [dict() for _ in range(N)]\n        ret = 1\n        for i in range(N):\n            for j in range(i + 1, N):\n                diff = A[j] - A[i]\n                dp[j][diff] = dp[i].get(diff, 1) + 1\n                ret = max(ret, dp[j][diff])\n        return ret\n        # def calc(A):\n        #     for i in range(len(A) - 1, -1, -1):\n        #         for j in range(i + 1, len(A)):\n        #             if A[j] < A[i]:\n        #                 continue\n        #             diff = A[j] - A[i]\n        #             memo[i][diff] = max(memo[i].get(diff, 0), memo[j].get(diff, 1) + 1)\n        #             ret = max(ret, memo[i][diff])\n        #     return ret\n        # \n        # return max(\n        #     calc(A), calc(list(reversed(A)))\n        # )\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())     ", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        \n        table = []\n        out = 0\n        for i in range(N):\n            table.append(defaultdict(dict))\n            for j in range(0, i):\n                js_table = table[j]\n                diff = A[i]-A[j]\n                if diff not in js_table:\n                    table[i][diff] = 2\n                else:\n                    table[i][diff] = table[j][diff]+1\n                \n                out = max(table[i][diff], out)\n                \n        # print(table)\n        return out", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[(j, A[j] - A[i])] = dp.get((i,A[j]-A[i]),1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        n = len(A)\n        for i in range(n):\n            for j in range(i+1, n):\n                dp[(j, A[j] - A[i])] = dp.get((i, A[j] - A[i]), 1) + 1\n                \n        return max(dp.values())\n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        ans = 0\n        n = len(A)\n        dp = [collections.defaultdict(int) for _ in range(n)]\n        for i in range(1, n):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                ans = max(ans, dp[i][diff])\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dic={}\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                dic[(j,A[j]-A[i])]=dic.get((i,A[j]-A[i]),1)+1\n        return max(dic.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                d = A[j]-A[i]\n                dp[j,d] = dp.get((i,d),1)+1\n                \n                \n        # print(dp)\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        N = len(A) \n        for i in range(N):\n            for j in range(i + 1, N):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())\n        # def calc(A):\n        #     for i in range(len(A) - 1, -1, -1):\n        #         for j in range(i + 1, len(A)):\n        #             if A[j] < A[i]:\n        #                 continue\n        #             diff = A[j] - A[i]\n        #             memo[i][diff] = max(memo[i].get(diff, 0), memo[j].get(diff, 1) + 1)\n        #             ret = max(ret, memo[i][diff])\n        #     return ret\n        # \n        # return max(\n        #     calc(A), calc(list(reversed(A)))\n        # )\n", "'''\n[9,4,7,2,10]\n i\n          j\n\n:  (freq, diff)\n{9: {0:1}, \n{4: {0,1}, {-5, 2}}\n{7: {0,1}, {-2,2}, {3,2}}\n{2: {0,1}, {-7:2}, {-2, 2}, {-5,2}}\n{10:{0,1}, {1, 2}, {6,2}, {3,3}}\n \n [20,1,15,3,10,5,8]\n  i       j\n  \n  {\n  20:(1,0)\n  1:(2,-19)\n  15:(2,-5)\n  3(4,5)\n  \n}\n\nSo we keep running hashmap of all numbers we meet along with its available arith difference along with its frequency. Then we do n^2 loop through all numbers, each time calculating the difference between number i and number j and trying to see if there is that difference avaible in the hashmap of j pointer. At this point if there is a difference match, we add on, else we start at frequency of.\n\nthen we can keep track of max throughout and return the max\n\nO(n^2)\nO(n^2)\n\n\n'''\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = collections.defaultdict(dict)\n        sol = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                curDiff = A[i] - A[j]\n                if curDiff in d[j]:\n                    d[i][curDiff] = d[j][curDiff] + 1\n                else:\n                    d[i][curDiff] = 2\n                    \n                sol = max(sol, d[i][curDiff])\n        return sol\n        \n            \n\n            \n", "class Solution:\n    def longestArithSeqLength(self, nums: List[int]) -> int:\n        n = len(nums)\n        \n        if n == 2:\n            return n\n        \n        dp = [{} for i in range(n)]\n        max_len = 0\n        for i in range(1, n):\n            for j in range(i):\n                diff = nums[i] - nums[j] \n                if diff in dp[j]:\n                    dp[i][diff] = max(2, 1 + dp[j][diff])\n                else:\n                    dp[i][diff] = 2\n                max_len = max(max_len, dp[i][diff])\n        \n        return max_len", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 0\n        dic = {}\n        for i in range(len(A)):\n            dic[i] = {}\n            for j in range(i):\n                key = A[i]-A[j]\n                if key in dic[j]:\n                    dic[i][key] = dic[j][key] + 1\n                else:\n                    dic[i][key] = 2\n                res = max(res, dic[i][key])\n        return res\n\n#print (Solution.longestArithSeqLength(Solution, [20,1,15,3,10,5,8]))\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A)<=2:\n            return len(A)\n        dp=[{} for i in range(len(A))]\n        ans=2\n        for i in range(1,len(A)):\n            for j in range(i):\n                diff=A[i]-A[j]\n                if diff in dp[j]:\n                    dp[i][diff]=dp[j][diff]+1\n                \n                else:\n                    dp[i][diff]=2\n                \n                ans=max(ans,dp[i][diff])\n        \n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                diff = A[j]-A[i]\n                dp[j,diff] = dp.get((i,diff),1)+1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {} #stores the longest len of arithmetic subsequence for each pair of (idx, diff)\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1 \n        return max(dp.values())\n    \n    #for A[i] and A[j], we store by ith idx and their diff the length of subsequence that follows diff A[j]-A[i]. Next time when cur A[j] becomes the first num and we find another item in A that's A[j]-A[i] away from cur A[j], we increment this subsequence's length by 1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [dict() for _ in range(n)] \n        max_len = 0\n        for i in range(n):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i][diff] = dp[j].get(diff, 1) + 1\n                # print(dp[j][diff])\n                max_len = max(max_len, dp[i][diff])\n        return max_len\n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[index][diff]\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j]- A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = collections.defaultdict(int)\n        \n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[(j, A[j] - A[i])] = dp[(i, A[j] - A[i])] + 1\n        \n        return max(dp.values()) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        longest = 2\n        dp = [{} for _ in range(n)]\n        \n        for i in range(1, n):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                \n                longest = max(longest, dp[i][diff])\n        \n        return longest\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        ans = 2\n        dp = [{} for _ in range(N)]\n        for i in range(N):\n            for j in range(i):\n                diff = A[j] - A[i]\n                dp[i][diff] = dp[j].get(diff,1)+1\n                ans = max(ans,dp[i][diff])\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        cache = dict()\n        maxi = 0\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                diff = A[j]-A[i]\n                if (diff, i) in cache:\n                    cache[diff, j]  = 1 + cache[diff, i]\n                else:\n                    cache[diff, j] = 2\n                if maxi < cache[diff, j]:\n                    maxi = cache[diff, j]\n        return maxi", "# class Solution:\n#     def longestArithSeqLength(self, arr: List[int]) -> int:\n#         if not arr:\n#             return 0\n        \n#         dp = [collections.defaultdict(lambda : 1) for _ in range(len(arr))]\n#         ret = 1\n#         for i in range(len(arr)):\n#             for j in range(i):\n#                 dp[i][arr[i] - arr[j]] = 1 + dp[j][arr[i] - arr[j]]\n#                 ret = max(ret, dp[i][arr[i] - arr[j]])\n        \n#         return ret\n\nclass Solution:\n   def longestArithSeqLength(self, A):\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, arr: List[int]) -> int:\n        dp = defaultdict(dict)\n        n = len(arr)\n        max_len = 0\n        for i in range(n):\n            for j in range(i):\n                diff = arr[i] - arr[j]\n                dp[i][diff] = dp[j].get(diff, 0) + 1\n                max_len = max(max_len, dp[i][diff])\n        \n        return max_len + 1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[i][k] = longestArithSeqLength(A[:i+1]) with step size k\n        dp = []\n        res = 0\n    \n        for i in range(len(A)):\n            step2len = defaultdict(int)\n            dp.append(step2len)\n            for prev_i in range(i):\n                step = A[i] - A[prev_i]\n                prev_step = dp[prev_i][step]\n                dp[i][step] = prev_step + 1\n                res = max(res, dp[i][step])\n        \n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        n = len(A)\n        ans = 0\n        \n        for _ in range(n):\n            dp.append({})\n        \n        for i in range(n):\n            for j in range(0, i):\n                if i == 0:\n                    dp[i][0] = 1\n                else:   \n                    diff = A[i] - A[j]\n\n                    if diff in dp[j]:\n                        dp[i][diff] = dp[j][diff] + 1\n                    else:\n                        dp[i][diff] = 2\n                        \n                    ans = max(ans, dp[i][diff])\n        \n        \n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        dp = [[2] * N for i in range(N)]\n        ans = 0\n        for i in range(N):\n            pos = {}\n            for j in range(i):\n                x = 2 * A[j] - A[i]\n                if x in pos:\n                    dp[i][j] = max(dp[i][j],1 + dp[j][pos[x]])\n                ans = max(ans,dp[i][j])\n                pos[A[j]] = j\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        idx_diff_count = {}\n        for i in range(1,len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if (j,diff) in idx_diff_count:\n                    idx_diff_count[i,diff] = idx_diff_count[j,diff] + 1\n                else:\n                    idx_diff_count[i,diff] = 2\n        return max(idx_diff_count.values())", "class Solution:\n    def longestArithSeqLength(self, A: 'List[int]') -> int:\n        n = len(A)\n        if n == 2:\n            return 2\n        dic = {0:{0:1}}\n        longest = 0\n        for i in range(1,n):\n            if i not in dic:\n                dic[i] = {0:1}\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff not in dic[j]:\n                    dic[i][diff] = 2\n                else:\n                    dic[i][diff] = dic[j][diff] + 1\n                longest = max(longest,dic[i][diff])\n        return longest", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        count = defaultdict(lambda: 1)\n        n = len(A)\n        for i in range(n):\n            for j in range(i + 1, n):\n                count[j, (A[j] - A[i])] = count[i, (A[j] - A[i])] + 1\n        return max(count.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        maxVal = 1\n        cur = collections.defaultdict(dict)\n        for i, val in enumerate(A):\n            for j in range(i):\n                dist = A[i] - A[j]\n                \n                cur[i][dist] = 1 + cur[j].get(dist, 1)\n                maxVal = max(maxVal, cur[i][dist])\n        return maxVal\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        n = len(A)\n        dp = {}\n        for i in range(n):\n            for j in range(i+1, n):\n                diff = A[j] - A[i]\n                if (i, diff) in dp:\n                    dp[(j, diff)] = dp[(i, diff)] + 1\n                else:\n                    dp[(j, diff)] = 2\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(dict)\n        \n        ans = 0\n        \n        dp[0][0] = 1\n        \n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i]-A[j]\n                dp[i][diff] = dp[j].get(diff, 1) + 1\n                ans = max(ans, dp[i][diff])\n                \n        return ans\n            \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = collections.defaultdict(int)\n        for i in range(1,len(A)):\n            for j in range(i):\n                dif = A[i] - A[j]\n                if (j,dif) in d:\n                    d[i,dif] = d[j,dif] + 1\n                else:\n                    d[i,dif] = 2\n        return max(d.values())", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # 3, 6, 9, 12\n        # 1,3 -> 0\n        # 2,6 -> 0\n        # 2,3 -> 1\n        # 3,9 -> 0\n        # 3,6 -> 1\n        # 3,3 -> 2\n        count = 2\n        diff_map = defaultdict(dict)\n        for i in range(len(A)):\n            for j in range(0, i):\n                #print(i, j, diff_map[i])\n                diff = A[i] - A[j]\n                diff_map[i][diff] = 1 + diff_map[j].get(diff, 1)\n                count = max(count, diff_map[i][diff])\n        return count\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if(len(A)==2):\n            return(len(A))\n        d=[{} for i in range(len(A))]\n        m=0\n        for i in range(1,len(A)):\n            for j in range(i):\n                k=A[i]-A[j]\n                if k in d[j]:\n                    d[i][k]=max(2,1+d[j][k])\n                else:\n                    d[i][k]=2\n                m=max(m,d[i][k])\n        return(m)\n        \n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        self.results = {}\n        # self.results[i][diff] is the length of the longest subsequence that includes index i to the end, with difference diff\n        \n        self.results[len(A) - 1] = {}\n        for i in range(len(A) - 2, -1, -1):\n            self.results[i] = {}\n\n            for j in range(i + 1, len(A), 1):\n                diff = A[i] - A[j]\n\n                possibility = self.results[j].get(diff, 1)                \n                if 1 + possibility > self.results[i].get(diff, 0):\n                    self.results[i][diff] = 1 + possibility\n\n        result = 1\n        for i in range(0, len(A) - 1, 1):\n            for value in list(self.results[i].values()):\n                result = max(result, value)\n        \n        return result\n            \n        \n        \n        \n        \n        \n        \n        \n    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        sequences = [defaultdict(int) for _ in range(len(A))]\n        \n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                sequences[i][diff] = max(sequences[j][diff]+1, sequences[i][diff])\n        \n        return max(max(mapping.values()) for mapping in sequences[1:]) + 1\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # find step between number, and count max of consequence index.\n        # step: [arr], # preserve order?\n        \n        if not A:\n            return 0\n        if len(A) == 2:\n            return 2\n        \n        dp = [{} for a in range(len(A))]\n        max_len = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = max(2, 1+dp[j][diff])\n                else:\n                    dp[i][diff] = 2\n                max_len = max(max_len, dp[i][diff])\n        return max_len", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        nums = A\n        n = len(nums)\n        if n == 2:\n            return n\n        dp = [{} for i in range(n)]\n        max_len = 0\n        for i in range(1, n):\n            for j in range(i):\n                diff = nums[i] - nums[j]\n                if diff in dp[j]:\n                    dp[i][diff] = max(2, 1 + dp[j][diff])\n                else:\n                    dp[i][diff] = 2\n                max_len = max(max_len, dp[i][diff])\n        return max_len", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) < 3:\n            return len(A)\n        d = {k:{} for k in set(A)}\n        for j in range(len(A)):\n            zero = d[A[j]].get(0, 0) + 1\n            for step, l in list(d[A[j]].items()):\n                d.setdefault(A[j] + step, {})\n                prev = d[A[j] + step].get(step, 0)\n                d[A[j] + step][step] = max(prev, l + 1)\n            for i in range(j):\n                d.setdefault(2 * A[j] - A[i], {})\n                d[2 * A[j] - A[i]].setdefault(A[j] - A[i], 2)\n                # print(i, j, d[2 * A[j] - A[i]])\n            d[A[j]] = {0: zero}\n            # print(d)\n        res = max([max(v.values()) for v in list(d.values())])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) <= 2:\n            return len(A)\n        \n        dp = [{} for _ in range(len(A))]\n        ans = 0\n        \n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                \n                if diff in dp[j]: # see if i's distance from j  is possible from a prev number to j\n                    dp[i][diff] = max(2, 1 + dp[j][diff])\n                else:\n                    dp[i][diff] = 2 # len 2 is always possible\n                    \n                ans = max(ans, dp[i][diff])\n                \n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        from collections import Counter, defaultdict\n        \n        min_val = min(A)\n        max_val = max(A)\n        \n        global_best = -1\n        dp = {}\n        \n        for i, v in enumerate(A):\n            dp[i] = {}\n            \n            # print(f'--------- PROCESSING INDEX {i}')\n            for j, w in enumerate(A[:i]):\n                d = v - w\n                dp[i][d] = dp[j].get(d, 1) + 1\n                global_best = max(global_best, dp[i][d])\n            \n        # print(dp)\n        \n        return global_best", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp = {}\n        # for i, a2 in enumerate(A[1:], start=1):\n        #     for j, a1 in enumerate(A[:i]):\n        #         print('a2: ' + str(a2) + '; a1: ' + str(a1))\n        #         d = a2 - a1\n        #         if (j, d) in dp:\n        #             dp[i, d] = dp[j, d] + 1\n        #         else:\n        #             dp[i, d] = 2\n        #         print(dp)\n        # return max(dp.values())\n        dp = {}\n        for i, a2 in enumerate(A[1:], start=1):\n            for j, a1 in enumerate(A[:i]):\n                d = a2 - a1\n                if (j, d) in dp:\n                    dp[i, d] = dp[j, d] + 1\n                else:\n                    dp[i, d] = 2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #nested dict: k1 = difference, k2 = end idx of difference, val = count\n        #max_len\n        record=defaultdict(lambda : defaultdict(int))\n        max_len = 0\n        #go over list\n        #   for num, get difference with all previous nums\n        #       if difference in dict and second key is the same as previous num, add to dict[dif][self] = count+1, update max_len\n        #return max_len\n        for i, v_i in enumerate(A):\n            for j in range(0, i):\n                dif = v_i - A[j]\n                if dif in record and j in record[dif]: record[dif][i] = record[dif][j]+1\n                else: record[dif][i] = 1\n                max_len = max(max_len, record[dif][i])\n        return max_len+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n\n        dp = collections.defaultdict(lambda: 1)\n        for i in range(n):\n            for j in range(i+1, n):\n                dp[(j, A[j]-A[i])] = dp[(i, A[j]-A[i])] + 1\n        return max(dp.values())\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        n = len(A)\n        for i in range(n):\n            for j in range(i+1, n):\n                dp[(j, A[j] - A[i])] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())\n", "from collections import OrderedDict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # 1. Compute pairwise differences d_ij: O(n^2)\n        #   [0 -5 -2 -7 1]\n        #   [0  0  3 -2 6]\n        #   [0  0  0 -5 3]\n        #   [0  0  0  0 8]\n        # 2. For each target node j in [1, n), record (diff, i) pairs where i in [0, n-1)\n        # 3. For each target node j in [1, n), LAS[j][diff] = LAS[i].get(diff, 0) + 1\n        # 4. Output max(LAS[n-1])\n        diffs = {}\n        for src in range(len(A) - 1):\n            for tgt in range(len(A) - 1, src, -1):\n                diff = A[tgt] - A[src]\n                # Only record the closest j to maximize LAS\n                diffs[src, diff] = tgt\n        \n        inbound_edges = {}\n        for edge, tgt in diffs.items():\n            inbound_edges.setdefault(tgt, []).append(edge)\n\n        max_length = 0\n        memo = {0: {}}\n        for tgt in range(1, len(A)):\n            memo[tgt] = {}\n            for src, diff in inbound_edges[tgt]:\n                seq_length = memo[src].get(diff, 1) + 1\n                if seq_length > memo[tgt].get(diff, 0):\n                    memo[tgt][diff] = seq_length\n                    max_length = max(seq_length, max_length)\n\n        return max_length", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(lambda : 1)\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                dp[j,A[j]-A[i]] = dp[i,A[j]-A[i]]+1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp=[{} for i in A]\n        if len(A)<=2:\n            return len(A)\n        ans=0\n        \n        for i in range(1,len(A)):\n            for j in range(i):\n                diff=A[i]-A[j]\n                if diff in dp[j]:\n                    dp[i][diff]=max(2,dp[j][diff]+1)\n                \n                else:\n                    dp[i][diff]=2\n                \n                ans=max(ans,dp[i][diff])\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) < 2:\n            return 0\n        la = len(A)\n        dp = {}\n        curr = 0\n        for i in range(1, la):\n            for j in range(i):\n                d = A[i] - A[j]\n                dp[(i, d)] = dp.get((j, d), 1) + 1\n                # if dp[(i, d)] > curr:\n                    # curr = dp[(i, d)]\n        return max(dp.values())", "from itertools import repeat\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = []\n        onright = list(repeat(0, 501))\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleftl.append(val)\n                onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if A is None or not A:\n            return 0\n        \n        N = len(A)\n        \n        f = [{} for _ in range(N)]\n        \n        ret = 0\n        \n        for i in range(1, N):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in f[j]:\n                    f[i][diff] = f[j][diff] + 1\n                else:\n                    f[i][diff] = 2\n                \n                ret = max(ret, f[i][diff])\n        \n        return ret", "class Solution:\n    def longestArithSeqLength(self, A):\n        n=len(A)\n        dp={}\n        for i in range(n):\n            for j in range(i+1,n):\n                dif = A[j]-A[i]\n                if (i,dif) in dp :\n                    dp[(j,dif)]=dp[(i,dif)]+1\n                else:\n                    dp[(j,dif)]=2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [{}]\n        max_length = 1\n        for i in range(1, len(A)):\n            dp.append({})\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                max_length = max(max_length, dp[i][diff])\n\n        return max_length", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        max_len = 2\n        alen = len(A)\n        \n        len_index = [{} for index in range(alen)]\n        len_index[1] = {A[1]-A[0]: 2}\n        for idx in range(2, alen):\n            val = A[idx]\n            indices = len_index[idx]\n            for preidx in range(idx):\n                diff = val - A[preidx]\n                if diff in len_index[preidx]:\n                    new_len = len_index[preidx][diff] + 1\n                else:\n                    new_len = 2\n                if diff in indices:\n                    indices[diff] = max(indices[diff], new_len)\n                else:\n                    indices[diff] = new_len\n                max_len = max(max_len, new_len)\n        return max_len\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i, a2 in enumerate(A[1:], start=1): #enumerate start=1 index \u8d77\u59cb\u6578\u503c\n            for j, a1 in enumerate(A[:i]):\n                d = a2 - a1\n                if (j, d) in dp:\n                    dp[i, d] = dp[j, d] + 1\n                else:\n                    dp[i, d] = 2 #\u5169\u500b\u6578 \u69cb\u6210\u4e00\u500bd\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(lambda: 1)\n        for first_index in range(len(A) - 1):\n            for second_index in range(first_index + 1, len(A)):\n                dp[(second_index, A[second_index] - A[first_index]\n                    )] = dp[(first_index, A[second_index] - A[first_index])] + 1\n        max_length = max(dp.values())\n        return max_length\n", "class Solution:\n    def longestArithSeqLength(self, A):\n        n = len(A)\n        dp = {}\n        for i in range(1, n):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if (j, diff) in dp:\n                    dp[(i, diff)] = dp[(j, diff)] + 1\n                else:\n                    dp[(i, diff)] = 2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        #dp[i] = {delta:len} \n        \n        n = len(A)\n        if n < 2 : return n\n        \n        dp = [ {} for _ in range(n)]\n        ans = 1\n        dp[0] = {0:1}\n        for i in range(1,n): \n            for j in range(i):\n                delta =  A[i] - A[j]\n                if delta in list(dp[j].keys()):\n                    dp[i][delta] = dp[j][delta] + 1\n                else:\n                    dp[i][delta] = 2\n                ans = max(ans,dp[i][delta])\n        \n        return ans\n                    \n                    \n                \n", "class Solution:\n    def longestArithSeqLength(self, nums: List[int]) -> int:\n        if not nums:\n            return 0\n        \n        n = len(nums)\n        # dp = [{}] * n # This is incorrect. It makes just one dictionary object, not n of them.\n        dp = [{} for _ in range(n)]\n        result = 2\n        \n        for i in range(1, n):\n            for j in range(i):\n                delta = nums[i] - nums[j]\n                longestArithSeq = 2\n                \n                # If we're adding on to the longest arithmetic sequence seen thus far.\n                if delta in dp[j]:\n                    longestArithSeq = dp[j].get(delta) + 1\n                    \n                # Add it to the dictionary.\n                dp[i][delta] = longestArithSeq\n                \n                # Update the result.\n                result = max(result, longestArithSeq)\n                \n                if result == 3:\n                    print('dim')\n        \n        return result", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = defaultdict(dict)\n        \n        n = len(A)\n        best = 0\n        \n        for i in range(1, n):\n            for j in range(0, i):\n                de = A[i] - A[j]\n                dp[i][de] = dp[j].get(de, 1) + 1\n                best = max(best, dp[i][de])\n                \n                \n        return best", "class Solution:\n    def longestArithSeqLength(self, nums: List[int]) -> int:\n        if not nums:\n            return 0\n        # Write your code here\n        memo = [0] * len(nums)\n        graph = collections.defaultdict(lambda: collections.defaultdict(int))\n        \n        res = 0\n        for i in range(1, len(nums)):\n            for j in range(i):\n                diff = nums[i] - nums[j]\n                prev_diffs = graph[j]\n    \n                prev_diff = prev_diffs[diff]\n                graph[i][diff] = prev_diff + 1\n                res = max(res, graph[i][diff])\n        return res + 1", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # lengths (i, d) longest arithmetic subsequence starting at i\n        # with difference d\n        lengths = defaultdict(lambda: 1)\n        \n        for i in range(len(A) - 2, -1, -1):\n            for j in range(len(A) - 1, i, -1):\n                lengths[i, A[j] - A[i]] = lengths[j, A[j] - A[i]] + 1\n        return max(lengths.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        from collections import defaultdict\n        dp = []\n        max_length = 0\n        for i in range(len(A)):\n            nd = defaultdict(int)\n            nd[0] = 1\n            dp.append(nd)\n            for j in range(i):\n                diff = A[i]-A[j]\n                dp[i][diff] = max(dp[j][diff],1)+1\n                if dp[i][diff] > max_length:\n                    max_length = dp[i][diff]\n        return max_length", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        n = len(A)\n        dp = [{} for i in range(n)]\n        ans = 0\n        for i in range(1, n):\n            for j in range(i):\n                diff = A[i] - A[j]\n                tmp = dp[j].get(diff, 0)\n                dp[i][diff] = tmp + 1\n                ans = max(ans, tmp+1)\n        return ans+1\n                    \n                    \n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) < 3:\n            return len(A)\n        res = 2\n        d = {k:{} for k in set(A)}\n        for j in range(len(A)):\n            zero = d[A[j]].get(0, 0) + 1\n            res = max(res, zero)\n            for step, l in list(d[A[j]].items()):\n                d.setdefault(A[j] + step, {})\n                prev = d[A[j] + step].get(step, 0)\n                d[A[j] + step][step] = max(prev, l + 1)\n                res = max(res, l + 1)\n            for i in range(j):\n                d.setdefault(2 * A[j] - A[i], {})\n                d[2 * A[j] - A[i]].setdefault(A[j] - A[i], 2)\n                # print(i, j, d[2 * A[j] - A[i]])\n            d[A[j]] = {0: zero}\n            # print(d)\n        # res = max([max(v.values()) for v in d.values()])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        if len(A)==1:\n            return 1\n        if len(A)==2:\n            return 2\n        N=len(A)\n        ans=2\n        seen={}\n        for i in range(N):\n            for j in range(i+1,N):\n                diff=A[j]-A[i]\n                # now for the rest can we do this diff?\n                \n                if (i,diff) in seen:\n                    ans=max(ans,seen[i,diff]+1)\n                    seen[j,diff]=seen[i,diff]+1\n                else:\n                    seen[j,diff]=2\n                \n                \n                # # prev=A[j]\n                # # for k in range(j+1,N):\n                # #     if A[k]-prev==diff:\n                # #         curr+=1\n                # #         prev=A[k]\n                # ans=max(ans,curr)\n                \n            # seen.add(A[i])\n        return ans\n                    \n        \n#         if N<=2:\n#             return N\n        \n#         dp=[0]*N\n        \n#         dp[2]=3 if A[2]-A[1]==A[1]-A[0] else 0\n        \n#         ans=dp[2]\n        \n#         for i in range(3,N):\n#             if A[i]-A[i-1]==A[i-1]-A[i-2]:\n#                 ans=max(ans,dp[i-1]+1,3)\n#                 dp[i]=max(1+dp[i-1],3)\n        \n#         return ans\n        \n#         N=len(A)\n#         def rec(diff,i,m):\n#             if (i,diff) in m:\n#                 return m[i,diff]\n            \n#             if i==N:\n#                 return 0\n            \n#             m[i,diff]=0\n            \n#             ans=0\n            \n#             for k in range(i+1,N):\n#                 if A[k]-A[i]==diff:\n#                     ans=1+rec(diff,k+1,m)\n#                     break\n            \n#             # make a new diff\n#             # ans=max(ans,rec(diff,i+1,m))\n            \n#             for k in range(i+1,N):\n#                 ans=max(ans,2+rec(A[k]-A[i],k+1,m))\n            \n#             m[i,diff]=ans\n#             return ans\n        \n#         return rec(float('inf'),0,{})\n            \n#             if A[i]-diff in seen and seen[A[i]-diff]==i-:\n#                 seen[A[i]]=i\n#                 print('found',A[i],diff,i,seen)\n#                 m[i,diff]=1+rec(diff,i+1,seen,m)\n#             else:\n#                 m[i,diff]=rec(diff,i+1,seen,m)\n            \n#             return m[i,diff]\n                \n            \n            \n        \n        \n#         N=len(A)\n#         ans=0\n#         m={}\n#         for i in range(N):\n#             for j in range(i+1,N):\n#                 diff=A[j]-A[i]\n#                 if diff!=0:\n#                     seen={A[j]:1, A[i]:0}\n#                     ans=max(ans,2+rec(diff,j+1,seen,m))\n        \n#         return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        max_len = 0\n        # index_of_array: {diff: curr_max_len}\n        dp = [{} for _ in range(len(A))]\n        \n        for i in range(1, len(A)):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                \n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                \n                max_len = max(max_len, dp[i][diff])\n        \n        return max_len\n", "from typing import Dict, Tuple\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n: int = len(A)\n        dp: Dict[Tuple[int, int], int] = {}\n        answer: int = 0\n        for i in range(n):\n            for j in range(i + 1, n):\n                diff: int = A[j] - A[i]\n                length: int = dp.get((i, diff), 1) + 1\n                dp[(j, diff)] = length\n                answer = max(answer, length)\n        return answer", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        m = max(A) - min(A)+1\n        n = len(A)\n        dp = [[1 for _ in range(2*m)] for _ in range(n)]\n        \n        ans = 0\n        for i in range(n):\n            for j in range(i+1, n):\n                d = A[j] - A[i]\n                \n                if d < 0:\n                    d = m + abs(d)\n                dp[j][d] = dp[i][d] + 1\n                ans = max(ans, dp[j][d])\n        \n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[i][d] = longestArithSeqLength(A[:i]) with difference d\n        # dp[i][d] = max(1 + dp[j][A[i]-A[j]] for j=[0..i-1])\n        # n^2\n        dp = dict()\n        max_len = 0\n        # [3,6,9,12]\n        # {1: {3: 2}, 2: {6: 2, 3: 3}, 3: {9: 2, 6: 2, 3: 4}}\n        # d = 3\n        for i in range(len(A)):\n            # dp[i] = {diff: max_len}\n            dp[i] = dict()\n            for j in range(i):\n                d = A[i]-A[j]\n                if d in dp[j]:\n                    dp[i][d] = max(dp[i][d], 1 + dp[j][d]) if d in dp[i] else 1 + dp[j][d]\n                else:\n                    dp[i][d] = 2\n                max_len = max(max_len, dp[i][d])\n    \n        return max_len\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [{} for i in range(n)] #i: {step: length}\n        for i in range(1, n):\n            for j in range(i):\n                step = A[i] - A[j]\n                dp[i][step] = max(\n                    dp[i].get(step, 0), dp[j].get(step, 1) + 1)\n                \n        return max(max(dp[i].values(), default=0)\n                   for i in range(n))", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        \n        dp = [{0: 1}]\n        \n        max_val = 1\n        \n        for i in range(1, len(A)):\n            dp.append({0: 1})\n            \n            i_val = A[i]\n            last_map = dp[-1]\n            \n            for j in range(i):\n                j_val = A[j]\n                diff = i_val - j_val\n                \n                diff_val = 2\n                \n                if diff in dp[j]:\n                    diff_val = dp[j][diff] + 1\n                \n                if diff not in last_map:\n                    last_map[diff] = diff_val\n                else:\n                    last_map[diff] = max(last_map[diff], diff_val)\n                \n                max_val = max(max_val, diff_val)\n                    \n        return max_val\n            \n                \n                \n        \n        # every sequence is trivially 1 for itself.\n        \n        \n        # start at index 1\n        \n            # loop up to this index\n                # get the difference of i and j\n                    \n                    # have we seen that difference before? if so, add a 1 to that amount.\n                    # if we have, take the max of the two times we've seen differences\n        \n        \n        # return the max difference we've seen so far.\n", "class Solution:\n    def longestArithSeqLength(self, arr: List[int]) -> int:\n        n = len(arr)\n        if n <= 2:\n            return n\n        dp = defaultdict(dict)\n        max_len = 2\n        for i in range(n):\n            for j in range(i + 1, n):\n                diff = arr[j] - arr[i]\n                if diff in dp[i]:\n                    dp[j][diff] = dp[i][diff] + 1\n                else:\n                    dp[j][diff] = 2\n                max_len = max(max_len, dp[j][diff])\n        return max_len", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        \n        m = {} # expect: {step: length}\n        for i in range(1, n):\n            e = m.pop(A[i], {})\n            for step, length in e.items():\n                e1 = m.setdefault(A[i] + step, {})\n                e1[step] = max(e1.get(step, 0), length + 1)\n            for j in range(i):\n                step = A[i] - A[j]\n                e1 = m.setdefault(A[i] + step, {})\n                e1[step] = max(e1.get(step, 0), 2)\n                \n        return max(max(e.values()) for e in m.values())", "class Solution:\n    def longestArithSeqLength(self, nums: List[int]) -> int:\n        n = len(nums)\n        if n == 2:\n            return n\n        dp = [{} for i in range(n)]\n        max_len = 0\n        for i in range(1, n):\n            for j in range(i):\n                diff = nums[i] - nums[j]\n                if diff in dp[j]:\n                    dp[i][diff] = max(2, 1 + dp[j][diff])\n                else:\n                    dp[i][diff] = 2\n                max_len = max(max_len, dp[i][diff])\n        return max_len", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        cur = collections.defaultdict(dict)\n        for i, v in enumerate(A):\n            for j in range(i):\n                val = v - A[j]\n                cur[i][val] = 1 + cur[j].get(val, 1)\n        return max(cur[i][j] for i in cur for j in cur[i])", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n\n        d=[{}]\n        ans=0\n        for i in range(1,len(A)):\n            d.append({})\n            for j in range(i):\n                if (A[j]-A[i]) in d[j]:\n                    d[i][A[j]-A[i]]=d[j][A[j]-A[i]]+1\n                else:\n                    d[i][A[j]-A[i]]=1\n                ans=max(ans,d[i][A[j]-A[i]])\n        return ans+1\n            \n                \n                \n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        if N <= 2:\n            return N\n        counters = [None] * N\n        ret_val = 2\n        for idx in range(N):\n            if idx == 0:\n                counters[idx] = Counter()\n                continue\n            counter = Counter()\n            for prev_idx in range(idx):\n                prev_counter = counters[prev_idx]\n                delta = A[idx] - A[prev_idx]\n                counter[delta] = max(\n                    counter[delta],\n                    max(prev_counter[delta] + 1, 2)\n                )\n                ret_val = max(ret_val, counter[delta])\n                counters[idx] = counter\n            # return\n        return ret_val\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        h = {}\n        h[0] = defaultdict(int)\n        res = 1\n        for i in range(1, len(A)):\n            h[i] = defaultdict(int)\n            for j in range(i):\n                diff = A[i] - A[j]\n                h[i][diff] = h[j][diff] + 1\n                res = max(res, h[i][diff])\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        h, res = {}, 1\n        for i in range(len(A)):\n            h[i] = defaultdict(int)\n            for j in range(i):\n                diff = A[i] - A[j]\n                h[i][diff] = h[j][diff] + 1\n                res = max(res, h[i][diff])\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        dp = [defaultdict(int) for _ in range(N)]\n        for j in range(N):\n            for i in range(j):\n                t = A[j] - A[i]\n                dp[j][t] = max(dp[j][t], dp[i][t] + 1)\n        return max(max(col.values()) for col in dp) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        m = 2\n        for i in range(1, len(A)):\n            for j in range(0, i):\n                if (j, A[i] - A[j]) in dp:\n                    dp[(i, A[i] - A[j])] = dp[(j, A[i] - A[j])] + 1\n                    m = max(m, dp[(i, A[i] - A[j])])\n                else:\n                    dp[(i, A[i] - A[j])] = 2\n        return m", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        n = len(A)\n        \n        res = 0\n        for r in range(1, n):\n            for l in range(r):\n                diff = A[r] - A[l]\n                \n                if (diff, l) in dp:\n                    dp[(diff, r)] = dp[(diff, l)] + 1\n                    res = max(res, dp[(diff, l)] + 1)\n                else:\n                    dp[(diff,r)] = 2\n                    res = max(res, 2)\n        return res\n                \n", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        mx = 0\n        dp = [defaultdict(int) for _ in range(len(A))]\n        for i,a in enumerate(A):\n            for j,b in enumerate(A[:i]):\n                if a - b in dp[j].keys():\n                    dp[i][a-b] = dp[j][a-b] + 1\n                else:\n                    dp[i][a-b] = 2\n                mx = max(mx, dp[i][a-b])\n        return mx", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = [{} for _ in range(len(A))]\n        res = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff not in dp[i]:\n                    dp[i][diff] = 2\n                if diff in dp[j]:\n                    dp[i][diff] = max(dp[i][diff], dp[j][diff] + 1)\n                res = max(res, dp[i][diff])\n        return res\n                \n\n                    \n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        N = len(A)\n        for i in range(N):\n            for j in range(i+1, N):\n                diff = A[j]-A[i]\n                if (i, diff) in dp:\n                    dp[(j, diff)] = dp[(i, diff)]+1\n                else:\n                    dp[(j, diff)] = 2\n                \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[i][d]: the length of longest arithmetic d subsequence in A[:i]\n        \n        dp = [collections.defaultdict(lambda: 1) for _ in range(len(A) + 1)]\n        \n        for i in range(1, len(A) + 1):\n            for j in range(1, i):\n                d = A[i-1] - A[j-1]\n                dp[i][d] = max(dp[i][d], dp[j][d] + 1)\n        \n        return max(max(list(item.values()) or [1]) for item in dp)\n", "from collections import defaultdict\nclass Solution:\n    \n    def longestArithSeqLength(self, A: List[int]) -> int:\n        def gen():\n            x = [0 for i in range(l)]\n            x[0] = 1\n            return x\n        \n        # dp[diff][index]\n        l = len(A)\n        dp = defaultdict(lambda: [1 for i in range(l)])\n        ans = 0\n        for i in range(l):\n            for j in range(i+1, l):\n                dp[A[j]-A[i]][j] = dp[A[j]-A[i]][i] + 1\n                ans = max(ans,dp[A[j]-A[i]][j] )\n\n            \n        return ans\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        memo = collections.defaultdict(dict)\n        ans = 0\n        \n        for i, a2 in enumerate(A):\n            for j in range(i):\n                a1 = A[j]\n                diff = a2 - a1\n                memo[i][diff] = 2\n                if diff in memo[j]:\n                    memo[i][diff] = max(memo[i][diff], memo[j][diff] + 1)\n                ans = max(ans, memo[i][diff])\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        seen = set()\n        dp = [defaultdict(int) for _ in range(len(A))]\n        mx = 0\n        \n        for i in range(len(A)):\n            seen.add(A[i])\n            for j in range(0, i):\n                ap = A[i] + -A[j]\n            \n                if A[i] + -ap in seen:\n                    dp[i][ap] = dp[j][ap] + 1\n                    mx = max(mx, dp[i][ap])\n                    \n        return mx + 1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A) \n        dp = [[2] * N for i in range(N)]\n        ans = 0\n        for i in range(N):\n            pos = {}\n            for j in range(i):\n                x = 2*A[j] - A[i]\n                if x in pos:\n                    dp[i][j] = max(dp[i][j], 1 + dp[j][pos[x]])\n                ans = max(ans, dp[i][j])\n                pos[A[j]] = j\n        \n        return ans\n    \n        dp = [dict() for _ in range(N)]\n        ret = 1\n        for i in range(N):\n            for j in range(i + 1, N):\n                diff = A[j] - A[i]\n                dp[j][diff] = dp[i].get(diff, 1) + 1\n                ret = max(ret, dp[j][diff])\n        return ret\n        # def calc(A):\n        #     for i in range(len(A) - 1, -1, -1):\n        #         for j in range(i + 1, len(A)):\n        #             if A[j] < A[i]:\n        #                 continue\n        #             diff = A[j] - A[i]\n        #             memo[i][diff] = max(memo[i].get(diff, 0), memo[j].get(diff, 1) + 1)\n        #             ret = max(ret, memo[i][diff])\n        #     return ret\n        # \n        # return max(\n        #     calc(A), calc(list(reversed(A)))\n        # )\n", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(dict) # { index: {difference: steps} }\n        max_cnt = 0\n        for i in range(1, len(A)):\n            for j in range(0, i):\n                difference = A[i] - A[j]\n                length = dp[j].get(difference, 1) + 1\n                dp[i][difference] = dp[j].get(difference, 1) + 1\n                max_cnt = max(max_cnt, length)\n        \n        return max_cnt\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        ans=0\n        dd=[[1]*1001 for i in range(len(A))]\n        for i in range(1,len(A)):\n            for j in range(i):\n                diff=A[i]-A[j]+500\n                dd[i][diff]=max(dd[i][diff],dd[j][diff]+1)\n                ans=max(ans,dd[i][diff])\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = {}\n        #dictionary of pairs (i pointer, dist)\n        \n        # i pointer iterates from 1index to the end\n        for i in range(1, len(A)):\n            # j pointer iterates from 0 to just left of i pointer then resets\n            for j in range(0, len(A[:i])):\n                \n                #finds the difference of the two values\n                d = A[i] - A[j]\n                \n                #checks to see if the same diff exists at j\n                if (j, d) in dp:\n                    #if j,d is in dp then add 1 because the value at i has the same difference and set that as i,d\n                    dp[i, d] = dp[j, d] + 1\n                #if not then its set to two because that accounts for the i,j as two integers\n                else:\n                    dp[i, d] = 2\n                    \n        #return what ever is the highest value of all the keys in the dictionary is\n        return max(dp.values())", "class Solution:\n                    \n    def longestArithSeqLength(self, A: List[int]) -> int:\n        memo = dict()\n        res = 0\n        for j in range(len(A)):\n            for i in range(j):\n                diff = A[j]-A[i]\n                if (diff, i) in memo:\n                    memo[(diff, j)] = memo[(diff, i)]+1\n                else:\n                    memo[(diff, j)] = 2\n        max_val = max(memo.values())\n        return max_val\n                    \n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n\n        # Stores [difference, index] -> length\n        # for each position j:\n        #   the arithmetic sequence \n        hash = {}\n\n        for i in range(length):\n            for j in range(i + 1, length):\n                hash[A[j] - A[i], j] = hash.get((A[j] - A[i], i), 1) + 1\n\n        # O(n)\n        return max(hash.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        memo = {}\n        for j, y in enumerate(A):\n            for i, x in enumerate(A[:j]):\n                d = y - x\n                memo[d, j] = memo.get((d, i), 1) + 1\n        return max(memo.values())", "#[] DP\n#O(N^2)\n#NOTE: diff can be either positive or negative\n#f[i]: the longest length of arithmetic subsequences who takes A[i] as the tail.\n#f[i] = defaultdict(lambda: 1)\n#f[i] = {diff: longest length}\n#f[i] = max(f[i][d], f[j][d] += 1) for j < i and d:=A[i]-A[j]\nfrom collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [defaultdict(lambda: 1) for _ in range(len(A))]\n        for i, a in enumerate(A):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i][diff] = max(dp[i][diff], dp[j][diff] + 1)\n        return max(max(lens.values()) for lens in dp)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) < 2: return len(A)\n        dp = {}\n        \n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j]-A[i]] = dp.get((i, A[j]-A[i]), 1) + 1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = []\n        maxv = 0\n        for i in range(n):\n            cur = A[i]\n            dp.append({})\n            for j in range(i):\n                pre = A[j]\n                dif = cur-pre\n                if dif not in dp[i]:\n                    dp[i][dif] = 0\n                if dif in dp[j]:\n                    dp[i][dif] = dp[j][dif] + 1\n                else:\n                    dp[i][dif] = 1\n                maxv = max(maxv, dp[i][dif])\n        return maxv+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        n = len(A)\n        for i in range(n):\n            for j in range(i+1, n):\n                dp[(j, A[j]-A[i])] = dp.get((i, A[j]-A[i]), 1)+1\n        return max(dp.values())", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [defaultdict(lambda: 1) for _ in range(n)]\n        for i in range(n):\n            for j in range(i):\n                dp[i][A[i]-A[j]]=max(dp[j][A[i]-A[j]]+1,2)\n        return max([val for _ in dp for __,val in _.items()])", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i,a1 in enumerate(A):\n            for j,a2 in enumerate(A[:i]):\n                diff = a1-a2\n                if (j,diff) in dp:\n                    dp[i,diff] = 1+dp[j,diff]\n                else:\n                    dp[i,diff]=2\n        return max(dp.values())\n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        for i in range(len(A)):\n            dp.append(collections.defaultdict(lambda: 1))\n            for j in range(i):\n                # calculate the differences\n                diff = A[i] - A[j]\n                # check the existing sequence length\n                dp[i][diff] = max(dp[i][diff], dp[j][diff]+1)\n        return max([max(d.values()) for d in dp])\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n\n    \n        f = {}\n        maxlen = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                #fff[(A[i], diff)] = max(fff[(A[i], diff)], fff.get((A[j], diff), 1) + 1)\n                #f[(i, diff)] = max(f[(i, diff)], f.get((j, diff), 1) + 1)\n                #f[(i, diff)] = f.get((j, diff), 1) + 1\n                \n                if (j, diff) not in f:\n                    f[i, diff] = 2\n                else:\n                    f[i, diff] = f[j, diff] + 1          \n                                    \n                maxlen = max(maxlen, f[(i, diff)])\n\n        return maxlen\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n=len(A)\n        dp={}\n        ans=0\n        for i in range(n):\n            for j in range(i):\n                diff=A[i]-A[j]\n                if (diff,j) not in dp:\n                    \n                    dp[(diff,i)]=2\n                else:\n                    \n                    dp[(diff,i)]=dp[(diff,j)]+1\n                ans=max(ans,dp[(diff,i)])       \n        return ans\n                    \n                    \n        \n        \n        \n", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dif = A[j]-A[i]\n                if (i,dif) in dp :\n                    dp[(j,dif)]=dp[(i,dif)]+1\n                else:\n                    dp[(j,dif)]=2\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 0\n        dic = {}\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                d = A[j]-A[i]\n                if (i,d) in dic:\n                    dic[(j,d)]=dic[(i,d)]+1\n                else:\n                    dic[(j,d)]=2\n                res = max(res,dic[(j,d)])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        '''\n        \n        '''\n        dp = {}\n        ans = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if (diff, j) not in dp:\n                    dp[(diff, i)] = 2\n                else:\n                    dp[(diff, i)] = dp[(diff, j)] + 1\n                ans = max(ans, dp[(diff, i)])\n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        \n        dp = [{} for _ in range(len(A))]\n                \n        max_seq = 1\n        \n        for i in range(1, len(A)):\n            dp[i] = {0:1}\n            \n            for j in range(i):\n                diff = A[i] - A[j]\n                \n                if diff not in dp[j]:\n                    dp[i][diff] = 2\n                else:\n                    dp[i][diff] = dp[j][diff] + 1\n                \n                max_seq = max(max_seq, dp[i][diff])\n                        \n        return max_seq\n            \n                \n                \n            \n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = {}\n        ans = 0\n        for i, n in enumerate(A):\n            for j in range(i):\n                diff = A[j] - A[i]\n                if (diff, j) not in dp:\n                    dp[(diff, i)] = 2\n                else:\n                    dp[(diff, i)] = dp[(diff, j)] +1\n                ans = max(ans, dp[(diff, i)])\n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        \n        for i, x in enumerate(A):\n            nd = collections.defaultdict(int)\n            dp.append(nd)\n            for j in range(i):\n                curr_diff = x - A[j]\n                dp[i][curr_diff] = dp[j][curr_diff] + 1\n          \n        return max(max(y.values()) for y in dp) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(int)\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                span = A[i] - A[j]\n                dp[j, span] = dp.get((i, span), 1) + 1\n        return max(dp.values())", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = {}\n        mval = -1\n        for i, n in enumerate(A):\n            for j in range(i):\n                if (j, n-A[j]) not in d:\n                    val = 2\n                else:\n                    val = d[(j, n-A[j])] + 1\n                mval = max(mval, val)\n                d[(i, n-A[j])] = val\n        return mval\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) <= 2:\n            return len(A)\n        dict = {}\n        maxLength = 2\n        for i in range(len(A) - 1):\n            sameNumEncountered = False\n            for j in range(i + 1, len(A)):\n                dif = A[j] - A[i]\n                if dif not in dict:\n                    dict[dif] = {}\n                \n                if A[i] not in dict[dif]:\n                    dict[dif][A[j]] = 2\n                elif dif != 0 or not sameNumEncountered:\n                    dict[dif][A[j]] = dict[dif][A[i]] + 1\n                \n                if dif == 0:\n                    sameNumEncountered = True\n                \n                maxLength = max(maxLength, dict[dif][A[j]])\n        return maxLength\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 0\n        \n        cache = dict()\n        \n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                diff = A[j] - A[i]\n                \n                if (i, diff) not in cache:\n                    cache[(j, diff)] = 2\n                else:\n                    cache[(j, diff)] = 1 + cache[(i, diff)]\n                \n                \n        return max(cache.values())\n'''\ndp[i][d] = longest subsequence ending at i with difference d\n\ndp[j][d] = 1 + max(\n    dp[j][A[j] - A[i]]\n) for j < i\n\n\n'''", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = collections.defaultdict(int)\n        mx = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                j_key = (j, diff)\n                i_key = (i, diff)\n                if j_key in dp:\n                    dp[i_key] = dp[j_key] + 1\n                else:\n                    dp[i_key] = 2\n                mx = max(mx, dp[i_key])\n        return mx#max(dp.values())\n", "'''\n\nbrute force - 2 for loops, find diff of i and i+1, then find next elem j where j-(i+i) is the same\nkeep track of max len found\n  t=O(n^3)\n  s=O(1)\n\n\n\n[20,1,15,3,10,5,8]\n                {}\n              {3:2}\n           {-5:2,-2:2}\n         {7:2,2:2,5:2}\n      {-12:2,-5:3,-10:2,-7:2}\n    \n                \n\n  |       |\n0,1,0,1,0,1\n          {}\n        {1:2}\n      {-1:2, 0:2}\n    {1:2,0:2}\n  {-1:2,0:3,}\nmax = 4\n-19\n\n\n'''\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        maps = [{} for i in range(len(A))]\n        maxlen = 0\n        for i in range(len(A)-1, -1, -1):\n            for j in range(i+1, len(A)):\n                diff = A[j] - A[i]\n                if diff in maps[j]:\n                    length = maps[j][diff]+1\n                else:\n                    length = 2\n                maxlen = max(maxlen, length)\n                if diff in maps[i]:\n                    prev_max = maps[i][diff]\n                    maps[i][diff] = max(prev_max, length)\n                else:\n                    maps[i][diff] = length\n        return maxlen\n                \ndef search(vals, start, target, length):\n    for i in range(start+1, len(vals)):\n        if vals[i] - vals[start] == target:\n            return search(vals, i, target, length+1)\n    return length", "from collections import OrderedDict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # When adding a new number A[j], we look at all previous numbers A[i]:\n        # (1) If A[j] can extend any arithmetic subsequence currently ends at A[i]: LAS += 1\n        # (2) Otherwise, LAS = 2\n        max_length = 0\n        subseq_lengths = {}\n        for j in range(1, len(A)):\n            for i in range(j):\n                diff = A[j] - A[i]\n                if (diff, i) in subseq_lengths:\n                    subseq_lengths[diff, j] = subseq_lengths[diff, i] + 1\n                else:\n                    subseq_lengths[diff, j] = 2\n                max_length = max(max_length, subseq_lengths[diff, j])\n        return max_length", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        from collections import defaultdict\n        dp = [Counter() for _ in range(len(A))]\n        \n        \n        max_length = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                # Was this difference earlier seen at index j? Then continue that chain\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else: # If no, create a new chain of length 2\n                    dp[i][diff] = 2\n                    \n                max_length = max(max_length, dp[i][diff])\n        # Print this table for any input for better understanding of the approach\n        # print table\n\n        return max_length\n        \n    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(1,len(A)):\n            for j in range(i):\n                delta = A[i] - A[j]\n                if (j, delta) in dp:\n                    dp[(i, delta)] = dp[(j, delta)] + 1\n                else:\n                    dp[(i,delta)] = 2\n        return max(dp.values())\n", "\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if A==[]: return 0\n        if len(A)==1: return 1\n        dp={}\n        for i in range(0,len(A)):\n            for j in range(0,i):\n                dp[i,A[i]-A[j]]=dp.get((j,A[i]-A[j]),0)+1\n        \n        return max(dp.values())+1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        ans = 0\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[i] - A[j]\n                if (i, diff) in dp:\n                    dp[(j, diff)] = dp[(i, diff)] + 1\n                else:\n                    dp[(j, diff)] = 2\n                ans = max(ans, dp[(j, diff)])\n        return ans\n", "class Solution:\n \n\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [{} for i in range(n)]\n        result = 2\n        \n        for i in range(1, n):\n            for j in range(i):\n                delta = A[i] - A[j]\n                \n                # If we've seen this delta with dp[j], then increase the length of the subseq by 1.\n                # This is equivalent of dp[i] 'adding on' to the subsequence.\n                if delta in dp[j]:\n                    currentLength = dp[j].get(delta)\n                    dp[i][delta] = currentLength + 1\n                \n                else:\n                    dp[i][delta] = 2\n                \n                result = max(result, dp[i][delta])        \n        return result\n    \n    def longestArithSeqLength2(self, A: List[int]) -> int:\n        from collections import Counter\n        cnt = Counter()\n        cnt\n        arith = [Counter() for i in range(len(A))]\n        longest = 0\n        for i in range(len(A)-2, -1,-1):\n            for j in range(i+1, len(A)):\n                diff = A[j]-A[i]\n                arith[i][diff] = max(1 + arith[j][diff], arith[i][diff])\n                longest = max(longest, arith[i][diff])\n        #print(arith)\n        \n        #         for i in range(len(A)):\n        #             #print(arith[i])\n        #             most_common = arith[i].most_common()\n\n        #             longest = max(most_common[0][1] if most_common else 0, longest)\n        return longest + 1\n        # for i in range(len(A)):\n        #     for j in range(i+1, len(A)):\n        #         cnt[A[j]-A[i]] += 1\n        #     print(A[i], cnt)\n        # print(cnt)\n        # val = cnt.most_common()[0][1]\n        # return val + 1 \n            \n        \n        #         self.arith = [dict() for i in range(len(A))]\n\n        #         def helper(i, diff):\n        #             if diff in self.arith[i]:\n        #                 return self.arith[i][diff]\n\n        #             val = 0\n        #             for j in range(i+1, len(A)):\n        #                 if A[j] - A[i] == diff:\n        #                     val = 1 + helper(j, diff)\n        #                     break\n        #             self.arith[i][diff] = val        \n        #             return self.arith[i][diff]\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        ret = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i, diff] = dp.get((j, diff), 1) + 1\n                ret = max(ret, dp[i, diff])\n        return ret", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [[0]*501 for i in range(n)]\n        max_val = 0\n        for i in range(n):\n            for j in range(i):\n                dif = A[i] - A[j]\n                dp[i][dif] = max(dp[i][dif], dp[j][dif] + 1)\n                max_val = max(dp[i][dif], max_val)\n        #print(dp)\n        return max_val + 1\n    \nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = collections.defaultdict(dict)\n        max_val = 0\n        for i in range(n):\n            for j in range(i):\n                dif = A[i] - A[j]\n                dp[dif].setdefault(i, 0)\n                dp[dif][i] = dp[dif].get(j,0) + 1\n                max_val = max(dp[dif][i], max_val)\n        #print(dp)\n        return max_val + 1    \n\nclass Solution1:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [collections.defaultdict(int) for _ in range(n)] \n        res = 0\n        for i in range(1, n):\n            for j in range(0, i):\n                diff = A[i]-A[j]\n                dp[i][diff] = dp[j][diff] + 1\n                    \n                res = max(res, dp[i][diff])\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[i][k] = longestArithSeqLength(A[:i+1]) with step size k\n        largest = max(A)\n        smallest = min(A)\n    \n        dp = dict()\n        res = 0\n    \n        for i in range(1, len(A)):\n            for prev_i in range(i):\n                step = A[i] - A[prev_i]\n                prev_step = dp[(prev_i, step)] if (prev_i, step) in dp else 1\n                dp[(i, step)] = prev_step + 1\n                \n                res = max(res, dp[(i, step)])\n        \n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n\n    \n        f = collections.defaultdict(int)\n        maxlen = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                #fff[(A[i], diff)] = max(fff[(A[i], diff)], fff.get((A[j], diff), 1) + 1)\n                #f[(i, diff)] = max(f[(i, diff)], f.get((j, diff), 1) + 1)\n                f[(i, diff)] = f.get((j, diff), 1) + 1\n                '''\n                if (j, diff) not in f:\n                    f[(i, diff)] = 2\n                else:\n                    f[(i, diff)] = max(f[(i, diff)],  f[(j, diff)] + 1)                \n                '''                    \n                maxlen = max(maxlen, f[(i, diff)])\n\n        return maxlen\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = list(dict() for i in range(len(A)))\n        maxsize = 0\n        for i in range(1,len(A)):\n            for j in range(0,i):\n                if(A[j] - A[i] in dp[j]):\n                    dp[i][A[j] - A[i]] = dp[j][A[j] - A[i]] + 1\n                else:\n                    dp[i][A[j] - A[i]] = 1\n            \n                maxsize = max(maxsize, dp[i][A[j] - A[i]])\n        \n        return maxsize + 1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        ret = 0\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[i] - A[j]\n                dp[j, diff] = dp.get((i, diff), 1) + 1\n                ret = max(ret, dp[j, diff])\n        return ret", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        maxd = max(A) - min(A)\n        dp = [[1 for j in range(2*maxd+1)] for i in range(len(A))]\n        maxv = 1\n        for i in range(1, len(dp)):\n            for j in range(i-1, -1, -1):\n                diff = A[i]- A[j]\n                dp[i][diff] = max(dp[i][diff], dp[j][diff] + 1)\n                maxv = max(maxv, dp[i][diff])\n        return maxv\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = dict()\n        \n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j]-A[i]] = dp.get((i, A[j]-A[i]), 1) + 1   # dp ending at j with common diff A[j]-A[i]\n                \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j]-A[i]), 1) + 1\n        return max(dp.values())\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[j] - A[i]\n                dp[j, diff] = dp.get((i, diff), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n\n    \n        f = {}\n        maxlen = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                #fff[(A[i], diff)] = max(fff[(A[i], diff)], fff.get((A[j], diff), 1) + 1)\n                #f[(i, diff)] = max(f[(i, diff)], f.get((j, diff), 1) + 1)\n                f[(i, diff)] = f.get((j, diff), 1) + 1\n                '''\n                if (j, diff) not in f:\n                    f[(i, diff)] = 2\n                else:\n                    f[(i, diff)] = max(f[(i, diff)],  f[(j, diff)] + 1)                \n                '''                    \n                maxlen = max(maxlen, f[(i, diff)])\n\n        return maxlen\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dir = {}\n        res = 0\n        for i in range(len(A)):\n            \n            for j in range(0, i):\n                diff = A[i] - A[j]\n                if (j, diff) not in dir:\n                    dir[(i, diff)] = 2\n                else:\n                    dir[(i, diff)] = dir[(j, diff)] + 1\n                    \n                res = max(res, dir[(i, diff)])\n        return res\n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) <= 2:\n            return len(A)\n        \n        dict = {}\n        maxLength = 2\n        for i in range(len(A) - 1):\n            sameNumEncountered = False\n            for j in range(i + 1, len(A)):\n                dif = A[j] - A[i]\n                \n                if (i, dif) not in dict:\n                    dict[j, dif] = 2\n                else:\n                    dict[j, dif] = dict[i, dif] + 1\n                maxLength = max(maxLength, dict[j, dif])\n                \n        return maxLength\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        mapping = {}\n        n = len(A)\n        if n < 2:\n            return n\n        max_ = 0\n        for i in range(n):\n            mapping[i] = {}\n            for j in range(i):\n                diff = A[i]-A[j]\n                if diff not in mapping[j]:\n                    mapping[i][diff] = 2\n                else:\n                    mapping[i][diff] = mapping[j][diff] + 1\n                max_ = max(max_, mapping[i][diff])\n        return max_\n            \n", "#SELF TRY 9/20/2020\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #Problem asks -> array A \n        #Want -> return the length of longest arithmetic subseq in A \n        \n        #Two cases basically.\n        #When building up to to your table \n        #When looking at previous values i.e from (col, change) we see that there is a PREVIOUS value already there. (With the same change) so you know you can extend that length by 1\n        #This \\\"1\\\" indicates the CUR number is being added because it's change is the same. \n        #If it's never been seen that the base case is at most length 2 \n        #i.e some \\\"subseq\\\" whatever it is with 2 elements would always be \\\"valid\\\" and so would be length 2 \n        table = dict()\n        longest_subseq = float('-inf')\n        \n        for row in range(len(A)): \n            for col in range(0, row): \n                change = A[row] - A[col]\n                if (col, change) in table: \n                    table[(row, change)] = 1 + table[(col, change)]\n                    \n                \n                \n                else: \n                    table[(row, change)] = 2 \n                    \n                longest_subseq = max(longest_subseq, table[(row, change)])\n                \n                \n                \n        return longest_subseq\n    \n    \n# class Solution:\n#     def longestArithSeqLength(self, A):\n#         d = {}\n#         for i in range(1, len(A)):\n#             for j in range(len(A)):\n#                 delta = A[i] - A[j]\n#                 d[(i, delta)] = d[(j, delta)] + 1 if (j, delta) in d else 2\n#         return max(d.values())\n# class Solution:\n#     def longestArithSeqLength(self, A: List[int]) -> int:\n#         #Array A \n#         #Want return the (Len of longest arithmetic subseq in A)\n#         #Recall that a subseq of A is a list A[i_1], A[i_2]... A[i_k] s.t\n#         # 0 <= i_1 < i_2 < i_k <= A.len() - 1 \n    \n#         #ROW\n#         # [9,4,7,2,10]\n        \n#         #COL\n#         #[\n#         #9\n#         #4\n#         #7 \n#         #2\n#         #10\n#         #]\n#         table = dict()\n#         for row in range(1, len(A)):\n#             for col in range(0, row): \n#                 change = A[row] - A[col]\n                \n#                 if (col, change) in table: \n#                     table[(row, change)] = table[(col, change)] + 1 \n#                 else: \n#                     #table(1, -5) = 2  \n#                     #table(1, 0) = 2 \n#                     #table(1, -3) = 2 + 1 \n#                     #table(1, -2) = 2\n#                     #table(1, -6) = 2\n#                     #table(0, -2) = 2\n#                     #(2, 0) = 2 \n#                     #(3,5) = 2 \n#                     #(2, -3) = 2 \n                    \n#                     table[(row, change)] = 2 \n            \n#         return max(table.values())\n", "#SELF TRY 9/20/2020\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #Problem asks -> array A \n        #Want -> return the length of longest arithmetic subseq in A \n        \n        #Two cases basically.\n        #When building up to to your table \n        #When looking at previous values i.e from (col, change) we see that there is a PREVIOUS value already there. (With the same change) so you know you can extend that length by 1\n        #This \\\"1\\\" indicates the CUR number is being added because it's change is the same. \n        #If it's never been seen that the base case is at most length 2 \n        #i.e some \\\"subseq\\\" whatever it is with 2 elements would always be \\\"valid\\\" and so would be length 2 \n        table = dict()\n        longest_subseq = float('-inf')\n        \n        for row in range(len(A)): \n            for col in range(0, row): \n                change = A[row] - A[col]\n                if (col, change) in table: \n                    table[(row, change)] = 1 + table[(col, change)]\n                    \n                \n                \n                else: \n                    table[(row, change)] = 2 \n                longest_subseq = max(longest_subseq, table[(row, change)])\n        return longest_subseq\n", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = defaultdict(dict)\n        ans = 0\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[j]-A[i]\n                if(diff in d[i]):\n                    d[j][diff] = d[i][diff]+1\n                else:\n                    d[j][diff] = 1\n                ans = max(ans, d[j][diff])\n        return ans+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # arr = {i:[1 for i in range(len(A))] for i in range(min(A)-max(A), max(A)-min(A)+1)}  # No need to initialize initially, will save lots of space\n        arr = {}\n        ans = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                dif = A[i]-A[j]\n                if not arr.get(dif):\n                    arr[dif] = [1 for i in range(len(A))]\n                arr[dif][i] = arr[dif][j] + 1\n                ans = max(ans, arr[dif][i])\n        \n        # for x in arr:\n        #     print(x, arr[x])\n        \n        return ans\n\n\n\n\n# TLE\n#         arr = {i:[1 for i in range(len(A))] for i in range(min(A)-max(A), max(A)-min(A)+1)}\n#         ans = 0\n#         for dif in range(min(A)-max(A), max(A)-min(A)+1):\n#             for j in range(1, len(arr[0])):\n#                 for k in range(j-1, -1, -1):\n#                     if A[j]-A[k] == dif:\n#                         arr[dif][j] = arr[dif][k] + 1\n#                         ans = max(ans, arr[dif][j])\n#                         break\n\n#         # for x in arr:\n#         #     print(x, arr[x])\n#         return ans\n        \n                        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        maximum = 1\n        for i in range(len(A)):\n            d = {}\n            dp.append({})\n            for j in range(i):\n                diff = A[i]-A[j]\n                if(diff not in dp[i]):\n                    dp[i][diff] = 0\n                if(diff not in dp[j]):\n                    dp[j][diff] = 0\n                dp[i][diff] = dp[j][diff]+1\n                maximum = max(maximum,dp[i][diff])\n        \n        return maximum+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 0\n        dic = {}\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                d = A[j]-A[i]\n                dic[(j,d)]=dic.get((i,d),1)+1\n                # if (i,d) in dic:\n                #     dic[(j,d)]=dic[(i,d)]+1\n                # else:\n                #     dic[(j,d)]=2\n                res = max(res,dic[(j,d)])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = {}\n        dp = {i:defaultdict(int) for i in range(len(A))}\n        mx = 0\n        \n        for i in range(len(A)):\n            if A[i] not in d: d[A[i]] = 1\n\n            for j in range(0, i):\n                ap = A[i] + -A[j]\n            \n            \n                if A[i] + -ap in d:\n                    dp[i][ap] = dp[j][ap] + 1\n                    mx = max(mx, dp[i][ap])\n                    \n        return mx+1 ", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        ans = 0\n        dp = {}\n        #dp[i,d] = max length subseq up to i with diff d\n        for i in range(len(A)):\n            for j in range(i):\n                d = A[i] - A[j]\n                if (j,d) in dp:\n                    dp[i,d] = dp[j,d] + 1\n                else:\n                    dp[i,d] = 2\n                ans = max(ans, dp[i,d])\n        return ans\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n                    \n        dp = [{} for _ in range(len(A))]\n        res = 0\n        for i in range(len(A)):            \n            for j in range(i):  \n                x = A[i] - A[j]                \n                dp[i][x] = max(dp[j][x]+1 if x in dp[j] else 0, 2 if x not in dp[i] else dp[i][x])\n                res = max(dp[i][x],res)\n                \n        #print(dp)\n        return res\n                \n        \n                \n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        for i, x in enumerate(A):\n            nd = collections.defaultdict(int)\n            dp.append(nd)\n            for j in range(i):\n                diff = x - A[j]\n                dp[i][diff] = dp[j][diff] + 1\n        return max(max(y.values()) for y in dp) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n#         dp = defaultdict(int)\n        \n#         for i in range(len(A)):\n#             for j in range(i+1, len(A)):\n#                 d = A[j] - A[i]\n                \n#                 if (i, d) in dp:\n#                     dp[j, d] = dp[i, d] + 1\n#                 else:\n#                     dp[j, d] = 2\n        \n#         return max(dp.values())\n\n        dp = {}\n        for i, Ai in enumerate(A):\n            for j in range(i+1, len(A)):\n                b = A[j] - Ai\n                if (i,b) not in dp: dp[j,b] = 2\n                else              : dp[j,b] = dp[i,b] + 1\n        return max(dp.values())\n", "class Solution2:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [[0]*501 for i in range(n)]\n        max_val = 0\n        for i in range(n):\n            for j in range(i):\n                dif = A[i] - A[j]\n                dp[i][dif] = max(dp[i][dif], dp[j][dif] + 1)\n                max_val = max(dp[i][dif], max_val)\n        #print(dp)\n        return max_val + 1\n    \nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = collections.defaultdict(dict)\n        max_val = 0\n        for i in range(n):\n            for j in range(i):\n                dif = A[i] - A[j]\n                dp[dif].setdefault(i, 0)\n                dp[dif][i] = dp[dif].get(j,0) + 1\n                max_val = max(dp[dif][i], max_val)\n        #print(dp)\n        return max_val + 1    \n\nclass Solution1:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [collections.defaultdict(int) for _ in range(n)] \n        res = 0\n        for i in range(1, n):\n            for j in range(0, i):\n                diff = A[i]-A[j]\n                dp[i][diff] = dp[j][diff] + 1\n                    \n                res = max(res, dp[i][diff])\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, nums: List[int]) -> int:\n        if not nums:\n            return 0\n        graph = collections.defaultdict(lambda: collections.defaultdict(int))\n        \n        res = 0\n        for i in range(1, len(nums)):\n            for j in range(i):\n                diff = nums[i] - nums[j]\n                prev_diffs = graph[j]\n    \n                prev_diff = prev_diffs[diff]\n                graph[i][diff] = prev_diff + 1\n                res = max(res, graph[i][diff])\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        ans = 0\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                b = A[j] - A[i]\n                if (i,b) not in dp: \n                    dp[(j,b)] = 2\n                else: \n                    dp[(j,b)] = dp[(i,b)] + 1\n                ans = max(ans, dp[(j, b)])\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(int)\n        \n        for i in range(1, len(A)):\n            for j in range(i):\n                d = A[i] - A[j]\n                \n                if (j, d) in dp:\n                    dp[i, d] = dp[j, d] + 1\n                else:\n                    dp[i, d] = 2\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        tracker = {}\n        max_length = 1\n        for i in range(n):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if (diff, j) in tracker:\n                    tracker[(diff, i)] = tracker[(diff, j)] + 1\n                else:\n                    tracker[(diff, i)] = 2\n                max_length = max(max_length, tracker[(diff, i)])\n        return max_length\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        if len(A) <= 2:\n            return len(A)\n        \n        dic = {}\n        \n        for idx, val in enumerate(A):\n            for j in range(idx + 1, len(A)):\n                diff = A[j] - val\n                if (diff, idx) in dic:\n                    dic[diff, j] = dic[(diff, idx)] + 1\n                else:\n                    dic[diff, j] = 2\n        return max(dic.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                diff = A[j] - A[i]\n                dp[(j, diff)] = dp.get((i, diff), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [[1 for i in range(1001)] for j in range(len(A))]\n        lastIndex = {}\n        ans = 1\n        for i,num in enumerate(A):\n            for j in range(1001):\n                commonDiff = j-500\n                lastNumIndex = lastIndex.get(num-commonDiff,-1)\n                if(lastNumIndex >= 0 and lastNumIndex < i):\n                    dp[i][j] = dp[lastNumIndex][j] + 1\n                    ans = max(ans,dp[i][j])\n            if(num not in lastIndex):\n                lastIndex[num] = -1\n            lastIndex[num] = i\n        return ans\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        cache = dict()\n        \n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                diff = A[j] - A[i]\n                \n                if (i, diff) not in cache:\n                    cache[(j, diff)] = 2\n                else:\n                    cache[(j, diff)] = 1 + cache[(i, diff)]\n                \n                \n        return max(cache.values())\n'''\ndp[i][d] = longest subsequence ending at i with difference d\n\ndp[j][d] = 1 + max(\n    dp[j][A[j] - A[i]]\n) for j < i\n\n\n'''", "class Solution:\n    #O(n^2) Bottom-up DP\n    def longestArithSeqLength(self, A):\n        dp = []\n        ans,n = 2,len(A)\n        for i in range(n):\n            dp.append(dict())\n            for j in range(i-1,-1,-1):\n                diff = A[i]-A[j]# create a start with A[i],A[j]\n                if diff not in dp[i]:\n                    dp[i][diff] = 2\n                # going backward to make sure information can be used!\n                if diff in dp[j]:\n                    dp[i][diff] = max(dp[j][diff]+1,dp[i][diff])\n                ans = max(ans,dp[i][diff])\n        return ans\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        M=max(A)+1\n        dp=[]\n        for i in range(len(A)):\n            temp=[1]*(M*2)\n            dp.append(temp)\n        m=0\n        for i in range(len(A)):\n            \n            for j in range(i):\n                delta=A[i]-A[j]\n                k=delta+M\n            #    print(k,M)\n                dp[i][k]=dp[j][k]+1\n                m=max(m,dp[i][k])\n  #      m=0\n  #      for i in range(2*M):\n  #          for j in range(len(A)):\n  #              m=max(dp[j][i],m)\n  #    #  print(dp)\n        return m", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # let dp[i][k] is the length of the arith seq with step size k\n        # range is -500, 500\n        n = len(A)\n        m = 1001\n        dp = [ [1] * (m+1) for _ in range(n)] # can replace it by a dict, will be quicker\n        ans = float('-inf')\n        for i in range(1,n):\n            for k in range(i):\n                dp[i][A[i]-A[k]+500] = max(dp[i][A[i]-A[k]+500], 1 + dp[k][A[i]-A[k]+500])  ## a_i - a_{i-1} = j \n            ans = max(max(dp[i]),ans)\n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}; n = len(A); res = 2\n        for i in range(n):\n            for j in range(i+1, n):\n                b = A[j] - A[i]\n                if (i,b) not in dp: dp[j,b] = 2\n                else              : dp[j,b] = dp[i,b] + 1\n                res = max(res, dp[j,b])\n        return res\n        #return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        cache = {}\n        ans = 0\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                # print(cache)\n                diff = A[j] - A[i]\n                if (i, diff) in cache:\n                    cache[j, diff] = cache[i, diff] + 1\n                    ans = max(cache[j, diff], ans)\n                else:\n                    cache[j, diff] = 2\n                    ans = max(cache[j, diff], ans)\n        return ans\n\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp, n, ans = {}, len(A), 0\n        for i in range(n):\n            for j in range(i+1, n):\n                diff = A[j] - A[i]\n                dp[(j, diff)] = dp.get((i, diff), 1) + 1\n                ans = max(ans, dp[(j, diff)])\n                \n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = {}\n        mostFreq = -1\n        for i in range(1,n):\n            for j in range(i-1, -1,-1):\n                diff = A[i] - A[j]\n                prevVal = 1\n                if (i,diff) in dp: prevVal = dp[ (i,diff)]\n                if (j,diff) in dp: \n                    dp[(i,diff)] = max(dp[(j,diff)], prevVal-1) + 1\n                else:\n                    if (i,diff) not in dp:\n                        dp[(i,diff)] = 1\n                # mostFreq = max(dp[(i,diff)], mostFreq)\n        ret = -1\n        for k,v in dp.items():\n            ret = max(ret,v)\n        return ret+1\n        return mostFreq+1", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = collections.defaultdict(int)\n        mx = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                j_key = (j, diff)\n                i_key = (i, diff)\n                \n                dp[i_key] = max(2, dp[j_key]+1)\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        n = len(A)\n        res = 2\n        for i in range(n):\n            for j in range(i):\n                d = A[i] - A[j]\n                dp[d, i] = dp.get((d, j), 1) + 1\n                res = max(res, dp[d, i])\n        return res\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if A is None or not A:\n            return 0\n        \n        N = len(A)\n        \n        f = [{} for _ in range(N + 1)]\n        \n        ret = 0\n        \n        for i in range(2, N + 1):\n            for j in range(1, i):\n                diff = A[i - 1] - A[j - 1]\n                if diff in f[j]:\n                    f[i][diff] = f[j][diff] + 1\n                else:\n                    f[i][diff] = 2\n                \n                ret = max(ret, f[i][diff])\n        \n        return ret", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = {}\n        for j in range(1,n):\n            for i in range(j):\n                diff = A[j] - A[i]\n                dp[j,diff] = max(dp.get((j,diff), 2), dp.get((i,diff),1)+1)\n                \n        return max(dp.values())", "class Solution:\n  def longestArithSeqLength(self, A: List[int]) -> int:\n    dp = {}\n    res = 0\n\n    for i in range(len(A)):\n      for j in range(0, i):\n        d = A[i] - A[j]\n        \n        dp[i, d] = dp.get((j, d), 1) + 1\n\n        res = max(res, dp[i, d])\n\n    return res        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # DP?\n        if len(A) <= 2: return len(A)\n        DP = [{} for i in range(len(A))]\n        ret = 0\n        for i in range(1, len(A)):\n            # j < i\n            for j in range(i):\n                diff = A[i] - A[j]\n                l = 0\n                if diff in DP[j]:\n                    l = DP[j][diff] + 1\n                else:\n                    l = 2 # A[j] and A[i]\n                new_longest = max(l, DP[i].get(diff, 0))\n                ret = max(ret, new_longest)\n                DP[i][diff] = new_longest\n        return ret", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = {}\n        m = 0\n        for i in range(len(A)):\n            for j in range(i):\n                d = A[i] - A[j]\n                j1 = (j,d)\n                i1 = (i,d)\n                if dp.get(j1) != None:\n                    dp[i1] = 1 + dp[j1]\n                else:\n                    dp[i1] = 1\n                m = max(m,dp[i1])\n        \n        return m+1\n", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:    \n        count = 0\n        memo = defaultdict(int)\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                num1 = A[i]\n                num2 = A[j]\n                diff = A[j] - num1\n                val = memo[(i,diff)] + 1\n                memo[(j, diff)] = val\n                count = max(val, count)\n        return count + 1 if count > 0 else count\n\n                    \n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                b = A[j] - A[i]\n                if (i,b) not in dp: \n                    dp[(j,b)] = 2\n                else: \n                    dp[(j,b)] = dp[(i,b)] + 1\n        return max(dp.values())", "class Solution:\n    #natural algorithm is O(n^3) by test for each i<j all remaining sequence\n    #O(n^2) Bottom-up DP\n    def longestArithSeqLength(self, A):\n        dp = []\n        ans,n = 2,len(A)\n        for i in range(n):\n            dp.append(dict())\n            for j in range(i-1,-1,-1):\n                diff = A[i]-A[j]# create a start with A[i],A[j]\n                if diff not in dp[i]:\n                    dp[i][diff] = 2\n                # going backward to make sure information can be used!\n                if diff in dp[j]:\n                    dp[i][diff] = max(dp[j][diff]+1,dp[i][diff])\n                ans = max(ans,dp[i][diff])\n        return ans\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[(j, A[j]- A[i])] = dp.get((i, A[j] - A[i]),1) + 1\n        return max(dp.values())\n            \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = dict()\n        max_val = 0\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n                if dp[j, A[j] - A[i]] > max_val:\n                    max_val = dp[j, A[j] - A[i]]\n        return max_val", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # table[index][diff] equals to the length of \n        # arithmetic sequence at index with difference diff.\n        table = dict()\n        max_v = 0\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                \n                _diff = A[j] - A[i]\n                if (i,_diff) in table.keys():\n                    table[j,_diff] = table[i,_diff] + 1\n                else:\n                    table[j,_diff] = 2 # the first diff\n                    # will corrspond to two values [v1,v2]\n                max_v = max(max_v,table[j,_diff])\n                    \n        return max_v", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i]-A[j]\n                dp[(i, diff)] = max(dp.get((i, diff), 2), dp.get((j, diff), 1)+1)\n\n        \n        # print(dp)\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        mp = defaultdict(list)\n        for i, a in enumerate(A):\n            mp[a].append(i)\n        \n        @lru_cache(None)\n        def dp(i, diff=None):\n            res = 1\n            if diff is None:\n                for j in range(i):\n                    res = max(res, 1+ dp(j, A[i] - A[j]))\n            else:\n                for j in mp[A[i] - diff]:\n                    if j < i: res = max(res, 1+ dp(j, diff))\n            return res\n        \n        return max([dp(i) for i in range(len(A))])\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        '''\n        dp = {}\n        for i in xrange(len(A)):\n            for j in xrange(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())\n        '''\n        l=len(A)\n        dp={}\n        c=collections.defaultdict(list)\n        for i in range(l-1):\n            for j in range(i+1,l):\n                #c[(i,j)]=A[i]-A[j]\n                c[i].append(A[i]-A[j])\n                dp[j,A[j]-A[i]]=dp.get((i,A[j]-A[i]),1)+1\n        #print(c)\n        return max(dp.values())\n        res=2\n        #for i in range(l-1):\n            #analyze c[i]\n            \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        n = len(A)\n        for i in range(n):\n            for j in range(i+1,n):\n                diff = A[i]-A[j]\n                dp[(diff, j)] = dp.get((diff,i), 1) + 1\n        #print(dp)\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A):\n        max_len, dp = 0, [{} for _ in range(len(A))]  # index_of_array: {diff: curr_max_len}\n        for i in range(1, len(A)):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 2\n                max_len = max(max_len, dp[i][diff] )\n        return max_len\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}  # key = (index, diff), value = len of sequences\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                curr_diff = A[j] - A[i]\n                dp[(j, curr_diff)] = dp.get((i, curr_diff), 1) + 1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [{} for i in range(len(A))]\n        res = 0\n        for i in range(len(A)):\n            for j in range(i):\n                # in the case that we are attaching to a single element\n                if A[i]-A[j] in dp[j]:\n                    dp[i][A[i]-A[j]] = max(2, 1 + dp[j][A[i]-A[j]])\n                else:\n                    dp[i][A[i]-A[j]] = 2\n                res = max(res, dp[i][A[i]-A[j]])\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = collections.defaultdict(int)\n        \n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i, diff] = max(2, dp[j, diff] + 1)\n        \n        return max(dp.values())", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        if n < 3:\n            return n \n        \n        dp = defaultdict(lambda: defaultdict(int))\n        res = 2\n        for i in range(1, n):\n            for j in range(i):\n                gap = A[i] - A[j]\n                dp[i][gap] = max(dp[j][gap] + 1, 2)\n                res = max(res, dp[i][gap])\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(int)\n        \n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                d = A[j] - A[i]\n                \n                if (i, d) in dp:\n                    dp[j, d] = dp[i, d] + 1\n                else:\n                    dp[j, d] = 2\n        \n        return max(dp.values())\n\n", "#[Runtime: 4284 ms, faster than 10.74%] DP\n#O(N^2)\n#NOTE: diff can be either positive or negative\n#f[i]: the longest length of arithmetic subsequences who takes A[i] as the tail.\n#f[i] = defaultdict(lambda: 1)\n#f[i] = {diff: longest length}\n#f[i] = max(f[i][d], f[j][d] + 1) for j < i and d:=A[i]-A[j]\nfrom collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(lambda: 1)\n        for i, a in enumerate(A):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if dp[i, diff] < dp[j, diff] + 1:\n                    dp[i, diff] = dp[j, diff] + 1\n        return max(dp.values())", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:    \n        count = 0\n        memo = defaultdict(int)\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                num1 = A[i]\n                num2 = A[j]\n                diff = A[j] - num1\n                val = memo[(num1,diff,i)] + 1\n                memo[(num2, diff,j)] = val\n                count = max(val, count)\n        return count + 1 if count > 0 else count\n\n                    \n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        dp = collections.defaultdict(int)\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[(j, A[j]-A[i])] = dp[(i, A[j]-A[i])] + 1\n        return max(dp.values()) + 1\n", "class Solution:\n    #    [9, 4, 7, 2, 10]\n    #     1  2  2  2   3\n    # (4, 5): 2\n    # (7, 2): 2\n    # (7, 3): 2\n    # (2, 7): 2\n    # (2, 2): 2\n    # (2, 5): 2\n    # (10, 1): 2\n    # (10, 6): 2\n    # (10, 3): 3\n    # (10, 8): 2\n    # store the last val in a sequence formed, its difference and length\n    \n    # [24, 13, 1, 100, 0, 94, 3, 0, 3] result = 2\n    # (13, 11): 2\n    # (1, 23): 2\n    # (1, 12): 2\n    # (100, 86): 2\n    # (100, 87): 2\n    # (100, 99): 2\n    # (0, -24): 2\n    # (0, -13): 2\n    # (0, -1): 2\n    # (0, -100): 2\n    # (94, 70): 2\n    # (94, 81): 2\n    # (94, 93): 2\n    # (94, -6): 2\n    # (94, 94): 2\n    # (3, -11): 2\n    # (3, -10): 2\n    # (3, -2): 2\n    # (3, -97): 2\n    # (3, 3, 7): 2\n    # (3, -91): 2\n    \n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        seqs = {} # stores mapping of (last_num_in_seq, diff) to length\n        \n        result = 2\n        \n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                \n                if (A[j], diff, j) in seqs:\n                    seqs[(A[i], diff, i)] = seqs[(A[j], diff, j)] + 1\n                else:\n                    seqs[(A[i], diff, i)] = 2\n                    \n                result = max(result, seqs[(A[i], diff, i)])\n                \n        return result", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = list(dict() for i in range(len(A)))\n        maxsize = 0\n        for i in range(1,len(A)):\n            for j in range(0,i):\n                diff = A[j] - A[i]\n                if(diff in dp[j]):\n                    dp[i][diff] = dp[j][diff] + 1\n                else:\n                    dp[i][diff] = 1\n            \n                maxsize = max(maxsize, dp[i][diff])\n        \n        return maxsize + 1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp, ans = [], 2\n        for i in range(len(A)):\n            dp.append({})\n            for j in range(i-1, -1, -1):                \n                dif = A[i]-A[j]\n                if dif not in dp[i]: dp[i][dif] = 2\n                if dif in dp[j]: dp[i][dif] = max(dp[j][dif]+1, dp[i][dif])\n                ans = max(ans, dp[i][dif])\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i):\n                dp[i, A[i] - A[j]] = dp.get((j, A[i] - A[j]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A)==0:\n            return 0\n        \n        ld=[{} for i in range(len(A))]\n        ans=0\n        ind=-1\n        for i in range(1,len(A)):\n            for j in range(i-1,-1,-1):\n                diff=A[i]-A[j]\n                if diff in ld[j]:\n                    # print(i,j,diff,ld[j][diff])\n                    if diff in ld[i]:\n                        ld[i][diff]=max(ld[j][diff]+1,ld[i][diff])\n                    else:\n                        ld[i][diff]=ld[j][diff]+1\n                else:\n                    if diff in ld[i]:\n                        ld[i][diff]=max(2,ld[i][diff])\n                    else:\n                        ld[i][diff]=2\n                if ld[i][diff]>ans:\n                    ind=i\n                ans=max(ans,ld[i][diff])\n                \n        # print(ind,ld[ind],A[ind],len(A))\n        \n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        maxd = max(A) - min(A)\n        dp = [[1 for j in range(2*maxd+1)] for i in range(len(A))]\n        maxv = 1\n        for i in range(1, len(dp)):\n            for j in range(i-1, -1, -1):\n                diff = A[i]- A[j]\n                dp[i][diff+maxd] = max(dp[i][diff+maxd], dp[j][diff+maxd] + 1)\n                maxv = max(maxv, dp[i][diff+maxd])\n        return maxv\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) <= 2:\n            return len(A)\n        idx_diff_to_longest = {}\n        longest = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                prev_len = idx_diff_to_longest[(j, diff)] if (j, diff) in idx_diff_to_longest else 1\n                idx_diff_to_longest[(i, diff)] = prev_len + 1\n                longest = max(longest, prev_len + 1)\n        return longest", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        A_indices = defaultdict(list)\n        for i, a in enumerate(A):\n            A_indices[a].append(i)\n        lengths = defaultdict(lambda: 2)\n        best = 2\n        \n        for i in range(len(A) - 3, -1, -1):\n            for j in range(i + 1, len(A) - 1):\n                if 2 * A[j] - A[i] in A_indices:\n                    indices = A_indices[2 * A[j] - A[i]]\n                    # find earliest occurrence of 2 * A[j] + A[i] after j\n                    if indices[-1] <= j:\n                        continue\n                    if indices[0] > j:\n                        r = 0\n                    else:\n                        l = 0\n                        r = len(indices) - 1\n                        while l < r - 1:\n                            mid = (l + r) // 2\n                            if indices[mid] <= j:\n                                l = mid\n                            else:\n                                r = mid\n                    lengths[i, j] = 1 + lengths[j, indices[r]]\n                    best = max(best, lengths[i, j])\n        return best", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #\u8fd9\u9898bottom up\u5199\u8d77\u6765\u5feb?\n        dp = defaultdict(lambda: 1) #default \u662f 1\uff0c \u81ea\u5df1\u80af\u5b9a\u662f\u81ea\u5df1\u7684seq\n        for i in range(len(A)):\n            for j in range(i): #\u4e4b\u524d\u7684\n                diff = A[i] - A[j]\n                dp[(i, diff)] = 1+ dp[(j, diff)]#\u52a0\u901f \u8fd9\u91cc\u4e0d\u9700\u8981\u7528max\u6765\u53d6\u56e0\u4e3a\u662f\u4ece\u524d\u5f80\u540e\u904d\u5386\uff0c\u6700\u540evalid\u7684diff\u80af\u5b9a\u662f\u6700\u5927\u7684\n                \n        return max(dp.values())", "class Solution:\n    #    [9, 4, 7, 2, 10]\n    #     1  2  2  2   3\n    # (4, 5): 2\n    # (7, 2): 2\n    # (7, 3): 2\n    # (2, 7): 2\n    # (2, 2): 2\n    # (2, 5): 2\n    # (10, 1): 2\n    # (10, 6): 2\n    # (10, 3): 3\n    # (10, 8): 2\n    # store the last val in a sequence formed, its difference and length\n    \n    # [24, 13, 1, 100, 0, 94, 3, 0, 3] result = 2\n    # (13, 11): 2\n    # (1, 23): 2\n    # (1, 12): 2\n    # (100, 86): 2\n    # (100, 87): 2\n    # (100, 99): 2\n    # (0, -24): 2\n    # (0, -13): 2\n    # (0, -1): 2\n    # (0, -100): 2\n    # (94, 70): 2\n    # (94, 81): 2\n    # (94, 93): 2\n    # (94, -6): 2\n    # (94, 94): 2\n    # (3, -11): 2\n    # (3, -10): 2\n    # (3, -2): 2\n    # (3, -97): 2\n    # (3, 3, 7): 2\n    # (3, -91): 2\n    \n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        seqs = {} # stores mapping of (last_num_in_seq, diff) to length\n        \n        result = 2\n        \n        for i in range(1, len(A)):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                \n                if (A[j], diff, j) in seqs:\n                    seqs[(A[i], diff, i)] = seqs[(A[j], diff, j)] + 1\n                else:\n                    seqs[(A[i], diff, i)] = 2\n                    \n                result = max(result, seqs[(A[i], diff, i)])\n                \n        return result", "from collections import Counter\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A)<=2:\n            return len(A)\n        dp = [Counter() for _ in range(len(A))]\n        ans = 2\n        for i in range(len(A)):\n            for j in range(i+1,len(A),1):\n                diff = A[j]-A[i]\n                if dp[i][diff] != 0:\n                    dp[j][diff] = dp[i][diff] + 1\n                else:\n                    dp[j][diff] = 2\n                ans = max(ans, dp[j][diff])\n        return ans\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        n = len(A)\n        for i in range(n):\n            for j in range(i+1, n):\n                dp[(j, A[j]-A[i])] = dp.get((i, A[j]-A[i]), 1) + 1\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = [{} for _ in range(len(A))]\n        longest = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                d[i][diff] = max(d[i].get(diff, 0), 1 + d[j].get(diff, 1))\n                longest = max(longest, d[i][diff])\n        return longest\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        if n < 3:\n            return n\n\n        counts = [{} for _ in A]\n        max_count = 0\n\n        for i in range(0, len(A)):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                counts[i][diff] = max(counts[i].get(diff, 1), counts[j].get(diff, 1) + 1)\n                max_count = max(max_count, counts[i][diff])\n\n        return max_count\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        dp = defaultdict(lambda: 1)\n        for j in range(N):\n            nxt = defaultdict(lambda: 1)\n            for i in range(j):\n                y, z = A[i], A[j]\n                d = delta = z - y\n                nxt[z, d] = max(nxt[z, d], dp[y, d] + 1)\n            dp.update(nxt)\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1;\n        return max(dp.values())", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, nums: List[int]) -> int:\n        dp = defaultdict(int)\n        n = len(nums)\n        max_val = 0\n        for i in range(n-1):\n            for j in range(i+1, n):\n                diff = nums[i] - nums[j]\n                dp[(j, diff)] = 1 + dp[(i, diff)]\n                max_val = max(max_val, dp[(j,diff)])\n        return max_val + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(0, i):\n                dp[i, A[i] - A[j]] = dp.get((j, A[i] - A[j]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        result = 1\n        n = len(A)\n        umap = [dict() for i in range(n)]\n        \n        for i in range(1, n):\n            for j in range(i):\n                diff = A[i] - A[j]\n                jval = (umap[j][diff] if diff in umap[j] else 0)\n                if diff not in umap[i]:\n                    umap[i][diff] = 0\n                umap[i][diff] = max(umap[i][diff], jval + 1)\n                result = max(result, umap[i][diff])\n        return result+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = [{} for _ in range(len(A))]\n        longest = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                d[i][diff] = max(d[i].get(diff, 0), 1 + d[j].get(diff, 1))\n                longest = max(longest, d[i][diff])\n        return longest", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        dp = defaultdict(lambda: 1)\n        for j in range(N):\n            dp2 = defaultdict(lambda: 1)\n            for i in range(j):\n                y, z = A[i], A[j]\n                d = delta = z - y\n                dp2[z, d] = max(dp2[z, d], dp[y, d] + 1)\n            dp.update(dp2)\n            # for k, v in dp.items():\n            #     print(k, v)\n            # print()\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[i][k] = longestArithSeqLength(A[:i+1]) with step size k\n    \n        dp = dict()\n        res = 0\n    \n        for i in range(1, len(A)):\n            for prev_i in range(i):\n                step = A[i] - A[prev_i]\n                prev_step = dp[(prev_i, step)] if (prev_i, step) in dp else 1\n                dp[(i, step)] = prev_step + 1\n                \n                res = max(res, dp[(i, step)])\n        \n        return res", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        A_indices = defaultdict(list)\n        for i, a in enumerate(A):\n            A_indices[a].append(i)\n        # lengths (i, d) longest arithmetic subsequence starting at i\n        # with difference d\n        lengths = defaultdict(lambda: 1)\n        best = 0\n        \n        for i in range(len(A) - 2, -1, -1):\n            for j in range(len(A) - 1, i, -1):\n                diff = A[j] - A[i]\n                lengths[i, diff] = lengths[j, diff] + 1\n                best = max(best, lengths[i, diff])\n        return best", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A)<=2:\n            return len(A)\n        \n        dp = [{} for _ in range(len(A))]\n        dp[1][A[1]-A[0]] = 2\n        \n        maxLen = 0\n        for i in range(2, len(A)):\n            for j in range(i):\n                diff = A[i]-A[j]\n                dp[i][diff] = max(dp[i][diff] if diff in dp[i] else 0, dp[j][diff]+1 if diff in dp[j] else 2)\n                maxLen = max(maxLen, dp[i][diff])\n        \n        return maxLen", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i in range(len(A)):\n            for j in range(i):\n                d = A[j] - A[i]\n                if (j, d) in dp:\n                    dp[(i, d)] = dp[(j, d)] + 1\n                else: \n                    dp[(i, d)] = 2\n                    \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                if (i, A[j] - A[i]) in dp:\n                    dp[(j, A[j] - A[i])] = dp[(i, A[j] - A[i])] + 1\n                else:\n                    dp[(j, A[j] - A[i])] = 2\n        return max(dp.values())", "# dict.get(key,number) return number if get None\n# \u7528dict \u7d00\u9304 dict[(id,diff)]: count\n# dict[(new id,diff)] = dict[(old id,diff)] || 1 + 1\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                x = dp.get((i, A[j] - A[i]), 1)\n                # print(x)\n                dp[j, A[j] - A[i]] = x + 1\n            # print(dp)\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i]-A[j]\n                dp[(i, diff)] = max(dp.get((i, diff), 2), dp.get((j, diff), 1)+1)\n\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [{} for i in range(n)]\n        result = 0\n        for i, a in enumerate(A):\n            for j in range(i):\n                l = dp[j].get(a - A[j], 1) + 1\n                dp[i][a - A[j]] = max(dp[i].get(a - A[j], 0) ,l)\n                result = max(result, l)\n        return result", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int: \n        if not A:\n            return 0\n        result = 0\n        N = len(A)\n        d =  collections.defaultdict(int)\n        for i in range(1,N):\n            for j in range(i):\n                diff = A[i] - A[j]\n                d[(i,diff)] = d[(j,diff)] +1\n                result = max(result, d[(i,diff)] )\n        return result+1\n        \n        \n \n        \n            \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        \n        diff_dict = {}\n        max_ = 0\n        for i in range(len(A)):\n            diff_dict[i] = {}\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in diff_dict[j]:\n                    diff_dict[i][diff] = diff_dict[j][diff] + 1\n                else:\n                    diff_dict[i][diff] = 1\n                    \n                max_ = max(max_, diff_dict[i][diff])\n             \n        return max_ + 1\n                \n                \n\n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n#         diff_dict = {}\n#         max_ = 0\n#         for i in range(len(A)):\n#             diff_dict[i] = {}\n#             for j in range(i):\n#                 diff = A[i] - A[j]\n                \n#                 if diff in diff_dict[j]:\n#                     diff_dict[i][diff] = diff_dict[j][diff] + 1\n                \n#                 else:\n#                     diff_dict[i][diff] = 1\n                \n                \n#                 max_ = max(max_, diff_dict[i][diff])\n                \n#         return max_\n                \n                \n                \n                \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n    \n#         diff_dict = {}\n#         max_ = 0\n#         for i in range(len(A)):\n#             diff_dict[i] = {}\n#             for j in range(i):\n#                 curr = A[i]\n#                 prev = A[j]\n\n#                 diff = curr - prev \n#                 if diff in diff_dict[j]:\n#                     diff_dict[i][diff] = diff_dict[j][diff] + 1\n#                 else:\n#                     diff_dict[i][diff] = 1\n\n#                 max_ = max(max_, diff_dict[i][diff])\n\n#         return (max_ + 1)\n\n                \n            \n            \n\n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n#         diff_dict = {}\n#         max_ = 0\n#         for i in range(len(A)):\n#             diff_dict[i] = {}\n#             for j in range(i):\n#                 curr = A[i]\n#                 prev = A[j]\n                \n#                 diff = curr - prev\n                \n#                 if diff in diff_dict[j]:\n#                     diff_dict[i][diff] =  diff_dict[j][diff] + 1\n#                 else:\n#                     diff_dict[i][diff] = 1\n                    \n#                 max_ = max(max_, diff_dict[i][diff])\n#         print(diff_dict)\n#         return (max_ + 1)\n            \n\n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n#         mapping = {}\n#         if len(A) < 2:\n#             return len(A)\n#         max_ = 0\n    \n#         for i in range(len(A)):\n#             mapping[i] = {}\n#             for j in range(0, i):\n#                 diff = A[i] - A[j]\n                \n#                 if diff not in mapping[j]:\n#                     mapping[i][diff] = 2\n#                 else:\n#                     mapping[i][diff] = mapping[j][diff] + 1\n#                 max_ = max(max_, mapping[i][diff])\n            \n#         return max_\n             \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n        \n#         track = [0 for i in range(len(A))]\n        \n#         for i in range(1, len(A)):\n#             track[i] = A[i] - A[i-1]\n#         print(track)\n        \n#         num = {}\n        \n#         for i in range(1, len(track)):\n#             val = (track[i] - track[i-1])\n#             if val in num:\n#                 num[val] += 1\n#             else:\n#                 num[val] = 1\n                \n#         print(num)\n#         return max(num.values()) + 1\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        sol = 1\n        l_d = [{} for _ in range(n)]\n        for j in range(1,n):\n            dj = l_d[j]\n            for i in range(j):\n                diff = A[j] - A[i]\n                di = l_d[i]\n                dj[diff] = max(dj.get(diff, 2), di.get(diff,1)+1)\n                sol = max(sol, dj[diff])\n                \n        return sol", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j]-A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # want subsequence that is longest arithmetic \n        # dictionary with len longest subsequence at j\n        # for i in range(A):\n        #    for j in range(i):\n        #        diff = j - i # difference between the two\n        #        dictionary[(diff, i)] = max(dictionary[(diff, i)], dictionary[(diff, j)]+1)\n        #        \n        \n        # (-5, 1) = 1\n        # (-2, 2) = 1\n        # (3, 2) = 1\n        # (-7,3) = 1\n        # (-2,3) = 1\n        long_len_sub_at_pos = {}\n        \n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                \n                # if (diff, j) in long_len_sub_at_pos:\n                #     long_len_sub_at_pos[(diff, i)] = max(long_len_sub_at_pos[(diff, i)], long_len_sub_at_pos[(diff, j)] + 1)\n                # else:\n                #     long_len_sub_at_pos[(diff, i)] = 2\n\n                sub_len_at_j = long_len_sub_at_pos.get((diff,j), 1) \n                long_len_sub_at_pos[(diff, i)] = max(long_len_sub_at_pos.get((diff,i), 0), sub_len_at_j + 1)\n        \n        #values in the dictionary would be the length of hte subseq\n        #loop over and find the max subseq\n        \n        return max(long_len_sub_at_pos.values())", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for endi, endv in enumerate(A[1:], start = 1):\n            for starti, startv in enumerate(A[:endi]):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for starti, startv in enumerate(A):\n            for endi, endv in enumerate(A[starti+1:], start = starti+1):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        N = len(A)\n        dp = [{0:1} for _ in range(N)]\n        for end in range(1, N):\n            for start in range(end):\n                diff = A[end] - A[start]\n                if diff in dp[start]:\n                    dp[end][diff] = dp[start][diff] + 1\n                else:\n                    dp[end][diff] = 2\n        return max(max(dp[end].values()) for end in range(1, N))\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        ans = 2\n        n = len(A)\n        index = {}\n        dp = [[2] * n for i in range(n)]\n        \n        for i, val in enumerate(A[:-1]):\n            for j in range(i+1, n):\n                first = val + val - A[j]\n                if first in index:\n                    dp[i][j] = dp[index[first]][i] + 1\n                    #ans = max(ans, dp[i][j])\n                    if dp[i][j] > ans: ans = dp[i][j]\n            index[val] = i\n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        seen = set()\n        for i in range(len(A)):\n            for j in range(i,len(A)):\n                seen.add(A[j] - A[i])\n        ans = 0\n        for j in seen:\n            d = defaultdict(int)\n            for i in A:\n                d[i] = d[i-j] + 1\n                ans = max(ans,d[i])\n        return ans\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        n = len(A)\n        ans = 0\n        for i in range(n - 1):\n            for j in range(i + 1, n):\n                d = A[j] - A[i]\n                dp[j, d] = dp.get((i, d), 1) + 1\n                ans = max(ans, dp[j, d])\n        \n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        if n < 3:\n            return n\n        self.memo = {}\n        self.res = 0\n        for i in range(n - 1):\n            for j in range(i + 1, n):\n                diff = A[j] - A[i]\n                if (i, diff) in self.memo:\n                    self.memo[(j, diff)] = self.memo[(i, diff)] + 1\n                else:\n                    self.memo[(j, diff)] = 2\n                self.res = max(self.res, self.memo[(j, diff)])\n        return self.res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        ans = 1\n        \n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[(j, A[j] - A[i])] = dp.get((i, A[j] - A[i]), 1) + 1\n                ans = max(ans, dp[(j, A[j] - A[i])])\n        \n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        from collections import Counter, defaultdict\n        \n        min_val = min(A)\n        max_val = max(A)\n        \n        global_best = -1\n        dp = {}\n        prev = defaultdict(list)\n        \n        for i, v in enumerate(A):\n            dp[i] = {}\n            \n            # print(f'--------- PROCESSING INDEX {i}')\n            for d in range(min_val-v, max_val-v+1):\n                # print(f'PROCESSING DIFF {d}')\n                if v+d < min_val or v+d > max_val:\n                    raise Exception()\n                \n                best = 0\n                \n                if v+d in prev:\n                    for j in prev[v+d]:\n                        best = max(best, dp[j].get(d, 1))\n            \n                dp[i][d] = best + 1\n                global_best = max(global_best, dp[i][d])\n            \n            prev[v].append(i)\n        # print(dp)\n        \n        return global_best", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        lim = len(A)\n        \n        d = dict()\n        ans = []\n        for i in range(0,lim):\n            d[i]=dict()\n            ans.append(2)\n        \n        for i in range(1,lim):\n            for j in range(0,i):\n                if A[i]-A[j] in d[j]:\n                    d[i][A[i]-A[j]]=d[j][A[i]-A[j]]+1\n                    if d[i][A[i]-A[j]]>ans[i]:ans[i] = d[i][A[i]-A[j]]\n                        \n                else:\n                    d[i][A[i]-A[j]] = 2\n        ###print(d)            \n        return max(ans)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp approach\n        \n        dp = {}\n        \n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n                \n        return max(dp.values())", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        largestLen = 0\n        \n        longestArithSequence = [defaultdict(lambda: 1) for _ in range(len(A))]\n        \n        for i in range(1, len(A)):\n            \n            for j in range(i):\n                diff = A[i] - A[j]\n                \n                seqLen = longestArithSequence[i][diff] = max(longestArithSequence[i][diff], longestArithSequence[j][diff] + 1)\n                \n                largestLen = max(largestLen, seqLen)\n                \n        \n        return largestLen\n                \n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        N = len(A)\n        for i in range(N):\n            for j in range(i+1, N):\n                dp[(j, A[j]-A[i])] = dp.get((i, A[j]-A[i]), 1)+1\n                \n        return max(dp.values())", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = {}\n        mval = -1\n        for i, n in enumerate(A):\n            for j in range(i):\n                if (j, n-A[j]) not in d:\n                    d[(i, n-A[j])] = 2\n                else:\n                    d[(i, n-A[j])] = d[(j, n-A[j])] + 1\n                mval = max(mval, d[(i, n-A[j])])\n        return mval\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 0\n        dp = collections.defaultdict(lambda: 1)\n        \n        for i in range(len(A)-1, -1, -1):\n            for j in range(i+1, len(A)):\n                dist = A[j] - A[i]\n                dp[(i, dist)] = max(dp[(i, dist)], dp[(j, dist)] + 1)\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        table = [collections.defaultdict(int) for _ in range(len(A))]\n        ans = 0\n        for i in range(len(A)):\n            for j in range(i):\n                if A[i] - A[j] in table[j]:\n                    currLen = table[j][A[i] - A[j]] + 1\n                else:\n                    currLen = 2\n                \n                ans = max(ans, currLen)\n                table[i][A[i]-A[j]] = max(table[i][A[i]-A[j]], currLen)\n        # print(table)\n        return ans\n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        \n        if len(A) < 3:\n            return len(A)\n        \n        sub_lens = [{} for i in A]\n        \n        max_len = 0\n        \n        for i in range(0, len(A)):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                sub_lens[i][diff] = max(sub_lens[i].get(diff, 1), sub_lens[j].get(diff, 1) + 1)\n                max_len = max(max_len, sub_lens[i][diff])\n                \n        return max_len", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = collections.defaultdict(int)\n        for i in range(len(A)):\n            for j in range(i):\n                dp[(i, A[i] - A[j])] = dp[(j, A[i] - A[j])] + 1\n        return max(dp.values())+1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        memo = {}\n        \n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                if (i, A[j] - A[i]) in memo:\n                    memo[j, A[j] - A[i]] = memo[i, A[j]-A[i]] + 1\n                else:\n                    memo[j, A[j] - A[i]] = 2\n                   \n        return max(memo.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = collections.defaultdict(lambda: collections.defaultdict(int))\n        \n        maxSoFar = 0\n        \n        for curr in range(len(A)):\n            for prev in range(curr):\n                difference = A[curr] - A[prev]\n                dp[curr][difference] = max(dp[curr][difference], dp[prev][difference] + 1)\n                maxSoFar = max(maxSoFar, dp[curr][difference])\n        \n        return maxSoFar + 1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        mem = [collections.defaultdict(int) for _ in A]\n        res = 1\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                v = A[j] - A[i]\n                mem[j][v]=max(mem[i][v] + 1, mem[j][v])\n                res = max(res, mem[j][v])\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = [collections.defaultdict(int) for _ in A]\n        res = 1\n        for i in range(0,len(A)):\n            for j in range(i):\n                v = A[i]-A[j]\n                d[i][v]=max(d[j][v]+1,d[i][v])\n                res = max(d[i][v],res)\n        return res+1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp=[]\n        m=0\n        for i,a in enumerate(A):        \n            dp.append(defaultdict(int))\n            for j in range(i):\n                dif=a-A[j]\n                dp[i][dif]=dp[j][dif]+1\n                m=max(m,dp[i][dif])\n        return m+1\n          \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        dp = [collections.defaultdict(int) for _ in A]\n        res = 1\n        for i in range(N):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if dp[i][diff] == None: dp[i][diff] = 0\n                if dp[j][diff] == None: dp[j][diff] = 0\n                \n                if dp[j][diff] == 0:\n                    dp[i][diff] = 2\n                else:\n                    dp[i][diff] = dp[j][diff] + 1\n                \n                res = max(res, dp[i][diff])\n        return res", "from collections import defaultdict\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        # lengths of AM seqs after i with a diff d\n        mem = [defaultdict(int) for _ in range(n)]\n        res = 0\n        for i in reversed(range(n)):\n            for j in range(i + 1, n):\n                d = A[j] - A[i]\n                mem[i][d] = max(1 + mem[j].get(d, 0), mem[i][d])\n                res = max(mem[i][d], res)\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) < 2:\n            return 0\n        la = len(A)\n        dp = {}\n        curr = 0\n        for i in range(1, la):\n            for j in range(i):\n                d = A[i] - A[j]\n                dp[(i, d)] = dp.get((j, d), 1) + 1\n                if dp[(i,d)] > curr:\n                    curr = dp[(i,d)]\n\n        return curr", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = [collections.defaultdict(int) for _ in A]\n        res = 1\n        for i in range(0,len(A)):\n            for j in range(i):\n                v = A[i]-A[j]\n                d[i][v]=max(d[j][v]+1,d[i][v])\n                res = max(d[i][v],res)\n        return res+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        memo = {}\n        for j, y in enumerate(A):\n            for i, x in enumerate(A[:j]):\n                d = y - x\n                memo[d, j] = memo.setdefault((d, i), 1) + 1\n        return max(memo.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        dp = [collections.defaultdict(int) for _ in range(N)]\n        res = 0\n        for i in range(1, N):\n            for j in range(i):\n                delta = A[i] - A[j]\n                dp[i][delta] = max(dp[i][delta], dp[j][delta] + 1, 2)\n                res = max(res, dp[i][delta])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A):\n        d = [collections.defaultdict(int) for _ in A]\n        res = 1\n        for i in range(0,len(A)):\n            for j in range(i):\n                v = A[i]-A[j]\n                d[i][v]=max(d[j][v]+1,d[i][v])\n                res = max(d[i][v],res)\n        return res+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        if len(A) == 0:\n            return 0\n        if len(A) == 1:\n            return 1\n        for i in range(1, len(A)):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                \n                if (j, diff) in dp:\n                    dp[(i, diff)] = 1 + dp[(j, diff)]\n                else:\n                    dp[i, diff] = 2\n        return max(dp.values())", "from collections import defaultdict\n\nclass Solution:\n    # @param A : tuple of integers\n    # @return an integer\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        sol = defaultdict(int)\n        \n        n = len(A)\n        for i in range(n):\n            for j in range(i):\n                d = A[i] - A[j]\n                sol[(i, d)] = max(sol[(i, d)], 1 + sol[(j, d)])\n                \n        return max(list(sol.values()), default=0) + 1\n    \n#         rows = len(A)\n#         cols = len(A)\n#         dp = [[2 for c in range(cols)] for r in range(rows)]\n        \n#         for c in range(cols): \n#             for r in range(0, c):\n                \n                \n#         for c in range(cols): \n#             for r in range(0, c):\n#                 diff = A[c] - A[r]             \n#                 x = A[r] - diff \n#                 # search for k such that dp[k][r] A[k]=x\n#                 for k in reversed(range(0, r)): # put this in doc\n#                     if(A[k] == x):\n#                         dp[r][c] = max(dp[k][r] + 1, dp[r][c])\n#                         break\n#         max_so_far = dp[0][0]\n        \n#         for c in range(cols): \n#             for r in range(0, c):\n#                 max_so_far = max(max_so_far, dp[r][c])\n        \n#         return max_so_far\n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        N = len(A)\n        dp = defaultdict(int)\n        for j in range(N):\n            for i in range(j):\n                t = A[j] - A[i]\n                dp[j, t] = max(dp[j, t], dp[i, t] + 1)\n        return max(dp.values()) + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #\u8fd9\u9898bottom up\u5199\u8d77\u6765\u5feb?\n        dp = defaultdict(lambda: 1) #default \u662f 1\uff0c \u81ea\u5df1\u80af\u5b9a\u662f\u81ea\u5df1\u7684seq\n        for i in range(len(A)):\n            for j in range(i): #\u4e4b\u524d\u7684\n                diff = A[i] - A[j]\n                dp[(i, diff)] = max(dp[(i, diff)], 1+ dp[(j, diff)])\n                \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A: return 0\n        dp = [{} for _ in range(len(A))]\n        res = 0\n        for j in range(1, len(A)):\n            for i in range(j):\n                if A[j]-A[i] in dp[i]:\n                    dp[j][A[j]-A[i]] = dp[i][A[j]-A[i]]+1\n                else:\n                    dp[j][A[j]-A[i]] = 1\n                res = max(dp[j][A[j]-A[i]], res)\n        return res+1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        res = 0\n        records = [collections.defaultdict(int) for _ in range(len(A))]\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                current = records[i].get(diff, 0)\n                prev = records[j].get(diff, 0) + 1\n                records[i][diff] = max(prev, current, 2)\n                res = max(res, records[i][diff])\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [collections.defaultdict(lambda: 1) for _ in range(n)]\n        ans = 0\n        for i in range(1, n):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                dp[i][diff] = max(dp[i][diff], dp[j][diff] + 1)\n                ans = max(ans, dp[i][diff])\n        return ans\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp = {}\n        # n = len(A)\n        # for i in range(1, n):\n        #     for j in range(i):\n        #         d = A[i] - A[j]\n        #         if (j, d) in dp:\n        #             dp[i, d] = dp[j, d] + 1\n        #         else:\n        #             dp[i, d] = 2\n        # return max(dp.values())\n        d = [collections.defaultdict(int) for _ in A]\n        res = 1\n        n = len(A)\n        for i in range(n):\n            for j in range(i):\n                v = A[i]-A[j]\n                d[i][v]=max(d[j][v]+1,d[i][v])\n                res = max(d[i][v],res)\n        return res+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        ans = 1\n        \n        for x in range(len(A)):\n            for y in range(x + 1, len(A)):\n                dp[y, A[y] - A[x]] = dp.get((x, A[y] - A[x]), 1) + 1\n                ans = max(dp[y, A[y] - A[x]], ans)\n                \n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            doneself = False\n            for lval in A[:i]:\n                diff = val - lval\n                nextval = val + diff\n                if nextval == val:\n                    if doneself:\n                        continue\n                    doneself = True\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A)<=2:\n            return len(A)\n            \n        seen = {}\n        ans = 0\n        for i in range(len(A)-1):\n            for j in range(i+1,len(A)):\n                seen[j, A[j]-A[i]] = seen.get((i, A[j]-A[i]), 0) + 1\n                ans = max(ans, seen[j, A[j]-A[i]])\n        \n        return ans+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n#         d = [collections.defaultdict(lambda:0) for _ in A]\n#         res = 1\n#         for i in range(len(A)):\n#             for j in range(i):\n#                 v = A[i]-A[j]\n#                 d[i][v]=max(d[j][v]+1,d[i][v])\n#                 res = max(d[i][v],res)\n#         return res+1\n    \n        d = [collections.defaultdict(lambda:0) for _ in A]\n        res = 1\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                v = A[i]-A[j]\n                d[j][v]=max(d[i][v]+1,d[j][v])\n                res = max(d[j][v],res)\n        return res+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        ans = 0\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                j1 = (j , diff)\n                i1 = (i, diff)\n                if dp.get(j1) != None:\n                    dp[i1] = 1 + dp[j1]\n                    ans = max(ans, dp[i1])\n                else:\n                    dp[i1] = 2\n                    ans = max(ans, dp[i1])\n\n        return ans\n            \n                    \n                \n", "\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        def helper():\n            return 1\n\n        dp = defaultdict(helper)\n        for i in range(len(A)):\n            for j in range(i):\n                step = A[i] - A[j]\n                dp[(i, step)] = max(dp[(j, step)] + 1, dp[(i, step)])\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = collections.defaultdict(int)\n        n = len(A)\n        for i in range(n):\n            for j in range(i + 1, n):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # longest length with end index i and diff j\n        if not A:\n            return 0\n        \n        dp = {}\n        n = len(A)\n        \n        max_len = 0\n        \n        for j in range(1, n):\n            for i in range(0, j):\n                if (i, A[j] - A[i]) in dp:\n                    dp[(j, A[j] - A[i])] = dp[(i, A[j] - A[i])] + 1\n                else:\n                    dp[(j, A[j] - A[i])] = 2\n                \n                max_len = max(max_len, dp[(j, A[j] - A[i])])\n                \n        return max_len\n                \n        \n                    \n                \n", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = collections.defaultdict(int)\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[(i, diff)] = max(2, dp[(i, diff)], dp[(j, diff)]+1)\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i, v in enumerate(A):\n            for j in range(i):\n                diff = v - A[j]\n                dp.setdefault((diff, i), 1)\n                dp[diff, i] = max(dp[diff, i], dp.get((diff, j), 1) + 1)\n        return max(dp.values(), default=0)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [{} for _ in range(len(A))]\n        max_len = 0\n        for i in range(len(A)-1):\n            \n            for j in range(i+1, len(A)):\n                diff = A[j] - A[i]\n                \n                if dp[i].get(diff) == None:\n                    dp[j][diff] = 2\n                else:\n                    dp[j][diff] = dp[i][diff] + 1\n                max_len = max(dp[j][diff], max_len)\n                \n        return max_len", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = []\n        maximum = 1\n        \n        for i in range(len(A)):\n            d={}\n            dp.append(d)\n            for j in range(i):\n                dp[i][A[i]-A[j]] = dp[j][A[i]-A[j]] = 0\n                \n        for i in range(len(A)):\n            d = {}\n            dp.append({})\n            for j in range(i):\n                diff = A[i]-A[j]\n                \n                dp[i][diff] = dp[j][diff]+1\n                maximum = max(maximum,dp[i][diff])\n        \n        return maximum+1", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [defaultdict(lambda: 1) for _ in range(n)]\n        ans = 2\n        for i in range(n):\n            for j in range(i):\n                dp[i][A[i]-A[j]]=max(dp[j][A[i]-A[j]]+1,2)\n                ans = max(ans,dp[i][A[i]-A[j]])\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = {}\n        mostFreq = -1\n        for i in range(1,n):\n            for j in range(i-1, -1,-1):\n                diff = A[i] - A[j]\n                prevVal = 1\n                if (i,diff) in dp: prevVal = dp[ (i,diff)]\n                if (j,diff) in dp: \n                    dp[(i,diff)] = max(dp[(j,diff)], prevVal-1) + 1\n                else:\n                    if (i,diff) not in dp:\n                        dp[(i,diff)] = 1\n                mostFreq = max(dp[(i,diff)], mostFreq)\n        # ret = -1\n        # for k,v in dp.items():\n        #     ret = max(ret,v)\n        # return ret+1\n        return mostFreq+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = {}\n        mostFreq = -1\n        for i in range(1,n):\n            for j in range(i-1, -1,-1):\n                diff = A[i] - A[j]\n                prevVal = 1\n                if (i,diff) in dp: prevVal = dp[ (i,diff)]\n                if (j,diff) in dp: \n                    dp[(i,diff)] = max(dp[(j,diff)], prevVal-1) + 1\n                else:\n                    if (i,diff) not in dp:\n                        dp[(i,diff)] = 1\n                mostFreq = max(dp[(i,diff)], mostFreq)\n        return mostFreq+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        from collections import defaultdict\n        opt = defaultdict(lambda : defaultdict(int))\n        l = len(A)\n        \n        sol = 0\n        # print(opt[-4])\n        \n        for i in range(l): # 6\n            # print(opt[-17])\n            for j in range(i + 1, l): # 8\n                diff = A[j] - A[i] # 0\n                \n                sub_l = 2                \n                if diff in opt[i]:\n                    sub_l = opt[i][diff] + 1\n                \n                opt[j][diff] = max(opt[j][diff], sub_l)\n                sol = max(sol, opt[j][diff])\n                # if opt[A[i]][diff] == 7:\n                #     print(i, A[i], diff)\n        # for i, row in enumerate(opt):\n        #      print(i, row)\n        \n        # print(dict(opt))\n        # print(opt[-4])\n        # for k, v in opt.items():\n        #     print(k, v) \n            # if k < 0:                \n            #     print(k, v) \n        #     pass\n        return sol\n                        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n            \n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n                \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[i][k] = longestArithSeqLength(A[:i+1]) with step size k\n        largest = max(A)\n        smallest = min(A)\n    \n        dp = dict()\n        res = 0\n    \n        for i in range(1, len(A)):\n            for prev_i in range(i):\n                step = A[i] - A[prev_i]\n                prev_step = dp[(prev_i, step)] if (prev_i, step) in dp else 1\n                if (i, step) not in dp:\n                    dp[(i, step)] = prev_step + 1\n                else:\n                    dp[(i, step)] = max(prev_step + 1, dp[(i, step)])\n                \n                res = max(res, dp[(i, step)])\n        \n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        from collections import Counter\n        cnt = Counter()\n        cnt\n        arith = [Counter() for i in range(len(A))]\n        for i in range(len(A)-2, -1,-1):\n            for j in range(i+1, len(A)):\n                diff = A[j]-A[i]\n                arith[i][diff] = max(1 + arith[j][diff], arith[i][diff])\n        #print(arith)\n        longest = 0\n        for i in range(len(A)):\n            #print(arith[i])\n            most_common = arith[i].most_common()\n            \n            longest = max(most_common[0][1] if most_common else 0, longest)\n        return longest + 1\n        # for i in range(len(A)):\n        #     for j in range(i+1, len(A)):\n        #         cnt[A[j]-A[i]] += 1\n        #     print(A[i], cnt)\n        # print(cnt)\n        # val = cnt.most_common()[0][1]\n        # return val + 1 \n            \n        \n#         self.arith = [dict() for i in range(len(A))]\n        \n#         def helper(i, diff):\n#             if diff in self.arith[i]:\n#                 return self.arith[i][diff]\n            \n#             val = 0\n#             for j in range(i+1, len(A)):\n#                 if A[j] - A[i] == diff:\n#                     val = 1 + helper(j, diff)\n#                     break\n#             self.arith[i][diff] = val        \n#             return self.arith[i][diff]\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = collections.defaultdict(dict)\n        max_val = 0\n        for i in range(n):\n            for j in range(i):\n                dif = A[i] - A[j]\n                dp[dif].setdefault(i, 0)\n                dp[dif][i] = max(dp[dif][i], dp[dif].get(j,0) + 1)\n                max_val = max(dp[dif][i], max_val)\n        #print(dp)\n        return max_val + 1", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for endi, endv in enumerate(A[1:], start = 1):\n            for starti, startv in enumerate(A[:endi]):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for starti, startv in enumerate(A):\n            for endi, endv in enumerate(A[starti+1:], start = starti+1):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        N = len(A)\n        dp = [{0:1} for _ in range(N)]\n        for end in range(1, N):\n            for start in range(end):\n                diff = A[end] - A[start]\n                if diff in dp[start]:\n                    dp[end][diff] = dp[start][diff] + 1\n                else:\n                    dp[end][diff] = 2\n        return max(max(dp[end].values()) for end in range(1, N))\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        ans = 2\n        n = len(A)\n        index = {}\n        dp = [[2] * n for i in range(n)]\n        \n        for i in range(n-1):\n            for j in range(i+1, n):\n                first = A[i] * 2 - A[j]\n                if first in index:\n                    dp[i][j] = dp[index[first]][i] + 1\n                    #ans = max(ans, dp[i][j])\n                    if dp[i][j] > ans: ans = dp[i][j]\n            index[A[i]] = i\n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #{#next num: cur length}\n        dp = [{} for a in A]\n        imax = 1\n        for i in range(len(A)):\n            for j in range(i-1,-1,-1):\n                diff = A[i]-A[j]\n                prev = dp[i].get(diff, 1)\n                dp[i][diff] = max(dp[j].get(diff, 1)+1, prev)\n                imax = max(imax, dp[i][diff])\n        return imax\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) <= 1:\n            return len(A)\n        \n#         memo = [(1 + 2 * 500) * [1] for _ in range(1 + len(A))]\n        \n#         res = 0\n        \n#         for i in range(len(A)-2, -1, -1):\n#             for j in range(i+1, len(A)):\n#                 diff = A[j] - A[i] + 500\n#                 memo[i][diff] = max(memo[i][diff], memo[j][diff] + 1)\n#                 res = max(res, memo[i][diff])\n        \n        h = dict()\n        \n        res = 0\n        \n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[j] - A[i]\n                h.setdefault((j, diff), 1)\n                h[(i, diff)] = h[(j, diff)] + 1\n                res = max(res, h[(i, diff)])\n    \n        return res", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp=dict()\n        mlen=0\n        for i in range(len(A)):\n            dp[A[i]]=defaultdict(lambda :1)\n            for j in range (i-1,-1,-1):\n                \n                d=A[i]-A[j]\n                if dp[A[i]][d]<=dp[A[j]][d]+1:\n                    dp[A[i]][d]=dp[A[j]][d]+1\n                    mlen=max(mlen,dp[A[i]][d])\n                    \n        return mlen\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #diffs = set(y - x for i, x in enumerate(A) for y in A[i+1:])\n        ans = 0\n        for diff in range(-500, 501):\n            data = {}\n            for num in A:\n                if num - diff not in data:\n                    if num not in data:\n                        data[num] = [num]\n                    continue\n                if len(data[num - diff]) < len(data.get(num, [])):\n                    continue\n                seq = data.pop(num - diff)\n                seq.append(num)\n                ans = max(ans, len(seq))\n                data[num] = seq\n        return ans", "import itertools\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = {}\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                check = (i,A[j]-A[i])\n                if check in d:\n                    d[(j,A[j]-A[i])] = d[check]+[A[j]]\n                else:\n                    d[(j,A[j]-A[i])] = [A[i],A[j]]\n        return len(max([i for i in list(d.values())],key=len))\n                \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:    \n        dp = [{} for i in range(len(A))]\n        res = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                dp[i][A[i]-A[j]] = max(dp[i].get(A[i]-A[j], 0), dp[j].get(A[i]-A[j], 0) + 1)\n                res = max(res, dp[i][A[i]-A[j]])\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [{} for _ in range(n)]\n        ans = 2\n        for i in range(1, n):\n            for j in range(i):\n                key_i = (A[i], A[i] - A[j])\n                key_j = (A[j], A[i] - A[j])\n                if key_i not in dp[i]:\n                    dp[i][key_i] = 2\n                if key_j in dp[j]:\n                    dp[i][key_i] = max(dp[i][key_i], dp[j][key_j] + 1)\n                ans = max(dp[i][key_i], ans)\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        M=max(A)+1\n        dp=[]\n        for i in range(len(A)):\n            temp=[1]*(M*2)\n            dp.append(temp)\n        for i in range(len(A)):\n            \n            for j in range(i):\n                delta=A[i]-A[j]\n                k=delta+M\n            #    print(k,M)\n                dp[i][k]=dp[j][k]+1\n        m=0\n        for i in range(2*M):\n            for j in range(len(A)):\n                m=max(dp[j][i],m)\n      #  print(dp)\n        return m", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 2\n        n = len(A)\n        dp = [collections.defaultdict(int) for _ in range(n)]\n        for i in range(1, n):\n            for j in range(i):\n                d = A[j] - A[i]\n                dp[i][d] = dp[j][d] + 1\n                res = max(res, dp[i][d]+1)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        seen = {}\n        res = 0\n        for i in range(len(A)):\n            for j in range(i-1, -1, -1):\n                diff = A[i] - A[j]\n                if diff not in seen:\n                    seen[diff] = {}\n                old_val = seen[diff][i] if i in seen[diff] else 0\n                if j not in seen[diff]:\n                    seen[diff][i] = 2\n                else:\n                    seen[diff][i] = seen[diff][j] + 1\n                seen[diff][i] = max(old_val, seen[diff][i])\n                if seen[diff][i] > res:\n                    res = seen[diff][i]\n        return res", "from collections import defaultdict\nclass Solution:\n    def find(self, A):\n        dp = []\n        ans = 1\n        for _ in A: dp.append({})\n        for i, ai in enumerate(A):\n            for j in range(i):\n                aj = A[j]\n                d = ai - aj\n                if d < 0: continue\n                if d not in dp[i]: dp[i][d] = 1\n                if d not in dp[j]: dp[j][d] = 1\n                temp = max(dp[i][d], dp[j][d] + 1)\n                dp[i][d] = temp\n                ans = max(ans, temp)\n        return ans\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        ans = self.find(A)\n        ans = max(ans, self.find(A[::-1]))\n        return ans;\n        \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        ans = 0\n        #A = sorted(A)\n        for diff in range(-500,501):\n            dp = defaultdict(int)\n            for e in A:\n                dp[e] = dp[e-diff]+1\n                ans = max(ans,dp[e])\n            \n        return ans\n                    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n#         dp = {} \n#         res = -1\n#         for i in range(0, len(A)):\n#             for j in range(i-1, -1, -1):\n#                 d = A[i] - A[j]\n#                 dp[(i, d)] = max(dp.get((i, d), -1), dp.get((j, d), 1) + 1)\n#                 res = max(res, dp[(i, d)])\n            \n#         return res\n        dp = [[-1 for i in range(1001)] for j in range(len(A))]\n        res = -1\n        for i in range(0, len(A)):\n            for j in range(i-1, -1, -1):\n                d = A[i] - A[j] + 500\n                if dp[j][d] == -1: dp[j][d] = 1\n                dp[i][d] = max(dp[i][d], dp[j][d] + 1)\n                res = max(res, dp[i][d])\n            \n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        hm = {A[0]:0}\n        for i in range(1, len(A)):\n            if A[i] not in hm:\n                hm[A[i]] = i\n            for j in range(i):\n                diff = A[i]-A[j]\n                # print(i, j, diff, dp, hm)\n                if (A[j]-diff) in hm:\n                    dp[(i, diff)] = max(dp.get((i, diff), 2), dp.get((j, diff), 1)+1)\n                else:\n                    dp[(i, diff)] = max(dp.get((i, diff), 2), 2)\n        \n        # print(dp)\n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:    \n        _max, _min = 0, 0\n        \n        for ele in A:\n            _max = max(_max, ele)\n            _min = max(_min, ele)\n            \n        diff = _min-_max\n        \n        dp = [ {} for i in range(len(A))]\n        res = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                dp[i][A[i]-A[j]] = max(dp[i].get(A[i]-A[j], 0), dp[j].get(A[i]-A[j], 0) + 1)\n                res = max(res, dp[i][A[i]-A[j]])\n        return res + 1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n     \n        f = collections.defaultdict(int)\n        fff = collections.defaultdict(int)\n        maxlen = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                #fff[(A[i], diff)] = max(fff[(A[i], diff)], fff.get((A[j], diff), 1) + 1)\n                f[(i, diff)] = max(f[(i, diff)], f.get((j, diff), 1) + 1)\n                '''\n                if (j, diff) not in f:\n                    f[(i, diff)] = 2\n                else:\n                    f[(i, diff)] = max(f[(i, diff)],  f[(j, diff)] + 1)                \n                '''                    \n                maxlen = max(maxlen, f[(i, diff)])\n\n        return maxlen\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        globalMax = 1\n        for i, a1 in enumerate(A):\n            for j, a2 in enumerate(A[:i]):\n                x = a1 - a2\n                if (j,x) in dp:\n                    dp[(i,x)] = dp[(j,x)] + 1\n                else:\n                    dp[(i,x)] = 2\n                globalMax = max(globalMax, dp[(i,x)])\n        return globalMax", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        N = len(A)\n        mx = 0\n        for i, n in enumerate(A):\n            for j in range(i+1, N):\n                b = A[j] - n\n                if (i, b) in dp:\n                    dp[j, b] = dp[i, b] + 1\n                else:\n                    dp[j, b] = 2\n                \n                mx = max(mx, dp[j, b])\n                    \n        return mx", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 0\n        dp = {i:collections.defaultdict(lambda: 1) for i in range(len(A))}\n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i]-A[j]\n                dp[i][diff] = max(dp[i][diff], dp[j][diff] + 1)\n                res = max(res, dp[i][diff])\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # dp[i][k] = longestArithSeqLength(A[:i+1]) with step size k\n        dp = dict()\n        res = 0\n    \n        for i in range(1, len(A)):\n            for prev_i in range(i):\n                step = A[i] - A[prev_i]\n                prev_step = dp[(prev_i, step)] if (prev_i, step) in dp else 1\n                dp[(i, step)] = prev_step + 1\n                \n                res = max(res, dp[(i, step)])\n        \n        return res", "from collections import defaultdict\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        def calc(A):\n            memo = [dict() for _ in range(len(A))]\n            ret = 1\n            for i in range(len(A) - 1, -1, -1):\n                for j in range(i + 1, len(A)):\n                    if A[j] < A[i]:\n                        continue\n                    diff = A[j] - A[i]\n                    memo[i][diff] = max(memo[i].get(diff, 0), memo[j].get(diff, 1) + 1)\n                    ret = max(ret, memo[i][diff])\n            return ret\n        \n        return max(\n            calc(A), calc(list(reversed(A)))\n        )", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 0\n        dp = [collections.defaultdict(lambda: 1) for _ in range(len(A))]\n        for i in range(len(A)):\n            for k in range(i-1, -1, -1):\n                dp[i][A[i] - A[k]] = max(dp[i][A[i]-A[k]], dp[k][A[i]-A[k]] + 1) # remember the MAX here!!!\n                res = max(res, dp[i][A[i]-A[k]])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        length = len(A)\n        \n        for i in range(length):\n            for j in range(i + 1, length):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        res = 0\n        dp = [collections.defaultdict(lambda: 1) for _ in range(len(A))]\n        for i in range(len(A)):\n            for k in range(i-1, -1, -1):\n                dp[i][A[i] - A[k]] = max(dp[i][A[i]-A[k]], dp[k][A[i]-A[k]] + 1)\n                res = max(res, dp[i][A[i]-A[k]])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) < 2:\n            return 0\n        la = len(A)\n        dp = {}\n        curr = 0\n        for i in range(1, la):\n            for j in range(i):\n                d = A[i] - A[j]\n                dp[(i, d)] = dp.get((j, d), 1) + 1\n                curr = max(curr, dp.get((i,d), 1))\n\n        return curr", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        if n == 2:\n            return n\n        dp = [{} for i in range(n)]\n        max_len = 0\n        for i in range(1,n):\n            for j in range(i):\n                diff = A[i]-A[j]\n                if diff in dp[j]:\n                    dp[i][diff] = max(2, 1+dp[j][diff])\n                else:\n                    dp[i][diff] = 2\n                max_len = max(max_len, dp[i][diff])\n        return max_len", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        \n        table = [ defaultdict(int)  for i in range(n) ]\n        out = 0\n        curr = A[1]-A[0]\n        full = True\n        for i in range(2,n):\n            if curr != A[i]-A[i-1]:\n                full = False\n                break\n        if full: return n\n        \n        # print(table)f\n        for i in range(n):\n            for j in range(0,i):\n                diff = A[i]-A[j]\n                if table[j][diff] == 0:\n                    table[j][diff] = 1\n                    \n                table[i][diff] = max(table[i][diff],table[j][diff] + 1)\n                \n                out = max(table[i][diff], out)\n        \n        # for i in range(n):\n        #     print(A[i], table[i])\n        return out\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = defaultdict(lambda:1)\n        \n        n = len(A)\n        for i in range(n):\n            for j in range(i):\n                dp[i, A[i] - A[j]] = max(dp[i, A[i] - A[j]], dp[j, A[i] - A[j]] + 1)\n        \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = [collections.Counter() for _ in A]\n        res = 1\n        for i in range(0,len(A)):\n            for j in range(i):\n                v = A[i]-A[j]\n                d[i][v]=max(d[j][v]+1,d[i][v])\n                res = max(d[i][v],res)\n        return res+1\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        d = {}\n        mx = 0\n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                if (diff, i) not in d:\n                    d[(diff, i)] = 2\n                if (diff, j) in d:\n                    d[(diff, i)] = max(d[(diff,i)], d[(diff, j)] + 1)\n                mx = max(mx, d[(diff, i)])\n        return mx\n                    \n        \n                    \n                \n        \n        \n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp = collections.defaultdict(dict)\n        ans = 0\n        \n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i][diff] = dp[i].get(diff, 0)\n                dp[i][diff]  = max(dp[i][diff], dp[j].get(diff,0)+1)\n                # if dp[i][diff]==6: print(A[i],A[j], diff)\n                ans = max(ans, dp[i][diff])\n        return ans+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) < 3:\n            return len(A)\n\n        n = len(A)\n\n        diff_c = collections.Counter()\n\n        for i in range(n):\n            for j in range(i):\n                if diff_c[(j, A[i]-A[j])] == 0:\n                    diff_c[(i, A[i]-A[j])] = 2\n                else:\n                    diff_c[(i, A[i] - A[j])] = diff_c[(j, A[i]-A[j])] + 1\n\n\n        return max(diff_c.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        #{#next num: cur length}\n        dp = {}\n        imax = 1\n        for i in range(len(A)):\n            for j in range(i-1,-1,-1):\n                diff = A[i]-A[j]\n                prev = dp.get((diff, i), 1)\n                saved = dp[(diff, i)] = max(dp.get((diff, j),1)+1, prev)\n                imax = max(imax, saved)\n        return imax\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        sol = 1\n        dp = {}\n        for j in range(1,n):\n            for i in range(j):\n                diff = A[j] - A[i]\n                dp[j,diff] = max(dp.get((j,diff), 2), dp.get((i,diff),1)+1)\n                sol = max(sol, dp[j,diff])\n                \n        return sol", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        pos = defaultdict(int)\n        best = 1\n        for i in range(len(A) - 1, -1, -1):\n            for j in range(i + 1, len(A)):\n                diff = A[j] - A[i]\n                if (j, diff) in pos:\n                    pos[(i, diff)] = max(pos[(i, diff)], pos[(j, diff)] + 1)\n                else:\n                    pos[(i, diff)] = max(pos[(i, diff)], 2)\n                best = max(best, pos[(i, diff)])\n        return best", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        \n        minimum = min(A)\n        maximum = max(A)\n        \n        length = 2*(maximum-minimum)+1\n        \n        dp = [[1 for i in range(length)] for j in range(len(A))]\n        \n        diff = maximum-minimum\n        ans = 0\n        for i in range(len(dp)):\n            for j in range(i):\n                dp[i][A[i]-A[j]+diff]=max(dp[i][A[i]-A[j]+diff],dp[j][A[i]-A[j]+diff]+1)\n                ans = max(ans,dp[i][A[i]-A[j]+diff])\n        return ans ", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        if len(A)==0:\n            return 0\n        \n        maxx=1\n        arSubCounts = dict()\n        \n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                diff = A[j]-A[i]\n                arSubCounts[(j,diff)]=max(arSubCounts.get((j,diff),1),arSubCounts.get((i,diff),1)+1)\n                maxx = max(arSubCounts[(j,diff)],maxx)\n                \n        return maxx\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        seen = {}\n        longest = 1\n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[j]-A[i]\n                if (i, diff) not in seen:\n                    seen[(i, diff)] = 1\n                    \n                if (j, diff) not in seen:\n                    seen[(j,diff)] = 1\n                \n                seen[(j,diff)] = max(seen[(j,diff)], seen[(i,diff)]+1)\n        \n        return max([v for k,v in seen.items()])", "import collections\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(len(A)):\n            for j in range(i + 1, len(A)):\n                dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1\n        return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        d = {}\n        res = 0\n        for i in range(n):\n            d[i] = {}\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff not in d[j]:\n                    d[i][diff] = 2\n                else:\n                    d[i][diff] = d[j][diff] + 1\n                res = max(res, d[i][diff])\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = collections.defaultdict(int)\n        ans = 0\n        \n        for i in range(len(A)):\n            for j in range(i):\n                diff = A[i] - A[j]\n                dp[i, diff] = max(2, dp[j, diff] + 1)\n                ans = max(ans, dp[i, diff])\n        \n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        \n        dp = {}\n        ans = 0\n        \n        for i in range(len(A)):\n            for j in range(i+1, len(A)):\n                diff = A[j] - A[i]\n                if (i, diff) not in dp: \n                    dp[(j, diff)] = 2 \n                else:\n                    dp[(j, diff)] = dp[(i, diff)] + 1\n                ans = max(ans, dp[(j, diff)])\n                    \n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        \n        # if there's no A, \n        if not A:\n            return 0\n        \n        # create a dp array,\n        # key would be a tuple of indices, \n        dp = {}\n        \n        # iterate over the length of A, \n        for it1 in range (1, len (A)):\n            \n            # iterate upto length of it1, \n            for it2 in range (it1):\n                \n                # create a tuple by difference, \n                key_tuple = (it2, A[it1] - A[it2])\n                \n                # if key_tuple doesn't exist, \n                if key_tuple not in dp:\n                    dp[(key_tuple)] = 1\n                    \n                dp [(it1, A[it1] - A[it2])] =  dp[(key_tuple)] + 1\n        #print (dp)  \n        return max (dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = [collections.defaultdict(int) for _ in range(len(A))]\n        res = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                dp[i][A[i]-A[j]] = max(dp[i][A[i]-A[j]], dp[j].get(A[i]-A[j], 1)+1)\n                res = max(res, dp[i][A[i]-A[j]])\n        return res", "'''\n\n[9,4,7,2,10]\n\n0, \n\n\n\n[3,6,9,12]\n\n0 -> 1\n\n3 -> 2\n\n3 -> 3\n\n6 -> 1\n\n\n\n'''\n\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        # if there's no list\n        if not A:\n            return 0\n        \n        if len(A) == 1:\n            return 1\n        \n        diff_map = {}\n        \n        # iterate over indexes\n        for it1 in range (1, len(A)):\n            \n            num1 = A[it1]\n            \n            for it2 in range (it1):\n                \n                num2 = A[it2]\n                \n                 # check the difference\n                diff = num1 - num2\n                \n                if (it2, diff) not in diff_map:\n                    diff_map[(it2, diff)] = 1\n                \n                diff_map[(it1, diff)] = diff_map[(it2, diff)] + 1\n                \n        #print (diff_map)   \n        # return the maximum of values\n        return max (diff_map.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp,res = {},0\n        for i in range(len(A)):\n            for j in range(i+1,len(A)):\n                dp[j,A[j]-A[i]] = dp.get((i,A[j]-A[i]),1)+1\n                res = max(res,dp[(j,A[j]-A[i])])\n        return res\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        \n        for i in range(1, len(A)):\n            for j in range(i):\n                diff = A[i]-A[j]\n                dp[(i, diff)] = max(dp.get((i, diff), 2), dp.get((j, diff), 1)+1)\n                \n        return max(dp.values())", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # -5:1\n        # -2:1 3:1 -5:1\n        # -7:1 -2:2 3:1 -5:2\n        # \n        if not A:\n            return 0\n        result = 0\n        N = len(A)\n        d= {}\n        for i in range(1,N):\n            for j in range(0,i):\n                diff = A[i] - A[j]\n                d[i,diff] = d.get((j,diff), 0 )+ 1\n                result = max(result,d[i,diff] )\n                \n        return result+1\n            \n", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for endi, endv in enumerate(A[1:], start = 1):\n            for starti, startv in enumerate(A[:endi]):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for starti, startv in enumerate(A):\n            for endi, endv in enumerate(A[starti+1:], start = starti+1):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        N = len(A)\n        dp = [{0:1} for _ in range(N)]\n        for end in range(1, N):\n            for start in range(end):\n                diff = A[end] - A[start]\n                if diff in dp[start]:\n                    dp[end][diff] = dp[start][diff] + 1\n                else:\n                    dp[end][diff] = 2\n        return max(max(dp[end].values()) for end in range(1, N))\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        ans = 2\n        n = len(A)\n        index = {}\n        dp = [[2] * n for i in range(n)]\n        \n        for i in range(n-1):\n            for j in range(i+1, n):\n                first = A[i] + A[i] - A[j]\n                if first in index:\n                    dp[i][j] = dp[index[first]][i] + 1\n                    #ans = max(ans, dp[i][j])\n                    if dp[i][j] > ans: ans = dp[i][j]\n            index[A[i]] = i\n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # this is very tricky, A inside can have duplicates\n        mem = {}\n        prev_values = set()\n        for idx, num in enumerate(A):\n            for pval in prev_values:\n                diff = num - pval\n                mem[num, diff] = max(mem.get((num, diff), 1), 1 + mem.get((pval, diff), 1))\n            prev_values.add(num)\n        return max(mem.values(), default = 0)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if len(A) < 2:\n            return len(A)\n        sequenceEnds = dict()\n        maxSequenceLen = 2\n        for i in range(len(A)):\n            seenDiffs = set()\n            for j in range(i):\n                diff = A[i] - A[j]\n                if diff in seenDiffs:\n                    continue\n                elif diff in sequenceEnds:\n                    sequencesWithDiff = sequenceEnds[diff]\n                    if A[j] in sequencesWithDiff:\n                        sequenceLength = sequencesWithDiff[A[j]]\n                        sequencesWithDiff[A[i]] = sequenceLength + 1\n                        maxSequenceLen = max(maxSequenceLen, sequenceLength + 1)\n                    else:\n                        sequencesWithDiff[A[i]] = 2\n                else:\n                    sequencesWithDiff = dict()\n                    sequencesWithDiff[A[i]] = 2\n                    sequenceEnds[diff] = sequencesWithDiff\n                seenDiffs.add(diff)\n        return maxSequenceLen\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        ans = 2\n        n = len(A)\n        index = {}\n        dp = [[2] * n for i in range(n)]\n        \n        for i in range(n-1):\n            for j in range(i+1, n):\n                first = A[i] * 2 - A[j]\n                if first in index:\n                    dp[i][j] = dp[index[first]][i] + 1\n                    ans = max(ans, dp[i][j])\n            index[A[i]] = i\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i in range(1, len(A)):\n            for j in range(i):\n                d = A[i] - A[j]\n                if (j, d) in dp:\n                    dp[i, d] = dp[j, d] + 1\n                else:\n                    dp[i, d] = 2\n        return max(dp.values())\n        \n        # dp = {}\n        # for i, a2 in enumerate(A[1:], start=1):\n        #     for j, a1 in enumerate(A[:i]):\n        #         d = a2 - a1\n        #         if (j, d) in dp:\n        #             dp[i, d] = dp[j, d] + 1\n        #         else:\n        #             dp[i, d] = 2\n        # return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        # subsequence problem -> dp\n        # dp[i][j] -- length of arithmetic subsequence ending at ith and jth element\n        ans = 2\n        n = len(A)\n        index = {}\n        dp = [[2] * n for i in range(n)]\n        \n        for i in range(n-1):\n            for j in range(i+1, n):\n                first = A[i] * 2 - A[j]\n                if first in index:\n                    dp[i][j] = dp[index[first]][i] + 1\n                    ans = max(ans, dp[i][j])\n            index[A[i]] = i\n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onleftl = []\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleftl:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                c = tex.get(diff, 2) + 1\n                if c > res:\n                    res = c\n                else:\n                    ending = (res - c) * diff + nextval\n                    if ending > 500 or ending < 0 or onright[ending] == 0:\n                        continue\n                toextend[nextval][diff] = c\n            if val not in onleft:\n                onleft.add(val)\n                onleftl.append(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        dp=[{} for i in range(len(A))]\n        for i in range(len(A)):\n            dp[i]={0:1}\n            for j in range(i):\n                diff=A[i]-A[j]\n                if diff in dp[j]:\n                    dp[i][diff]=dp[j][diff]+1\n                else:\n                    dp[i][diff]=2\n            \n        ans=0\n        for dic in dp:\n            if dic:\n                ans=max(ans,max(dic.values()))\n            \n        #print(dp)\n        return ans", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        \n        dp = [{} for _ in range(n)]\n        \n        ans = 0\n        \n        for i in range(n):\n            dp[i][0] = 1\n            for j in range(i):\n                diff = A[i] - A[j]\n                \n                if diff not in dp[j]:\n                    dp[i][diff] = 2\n                else:\n                    dp[i][diff] = dp[j][diff] + 1\n            \n            ans = max(ans, max(dp[i].values()))\n        \n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for endi, endv in enumerate(A[1:], start = 1):\n            for starti, startv in enumerate(A[:endi]):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for starti, startv in enumerate(A):\n            for endi, endv in enumerate(A[starti+1:], start = starti+1):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        N = len(A)\n        dp = [{0:1} for _ in range(N)]\n        for end in range(1, N):\n            for start in range(end):\n                diff = A[end] - A[start]\n                if diff in dp[start]:\n                    dp[end][diff] = dp[start][diff] + 1\n                else:\n                    dp[end][diff] = 2\n        return max(max(dp[end].values()) for end in range(1, N))\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        ans = 2\n        n = len(A)\n        index = {}\n        dp = [[2] * n for i in range(n)]\n        \n        for i in range(n-1):\n            for j in range(i+1, n):\n                first = A[i] * 2 - A[j]\n                if first in index:\n                    dp[i][j] = dp[index[first]][i] + 1\n                    ans = max(ans, dp[i][j])\n            index[A[i]] = i\n        return ans\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        biggest = max(A)\n        \n        def findLen(A: List[int]) -> int:\n            seen = set()\n            dp = defaultdict(lambda: defaultdict(lambda: 0)) \n            \n            for a in A:\n                for prev in seen:\n                    gap = a - prev\n                    newLen = 2 if dp[gap][prev] == 0 else 1 + dp[gap][prev]\n                    dp[gap][a] = max(dp[gap][a], newLen)\n                    \n                seen.add(a)\n            \n            return max([l for gaps in dp.values() for l in gaps.values()])\n                \n        \n        return findLen(A)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        if not A:\n            return 0\n        n = len(A)\n        if n < 2:\n            return 0\n\n        res = 2\n        dp = [{} for i in range(n)]\n        dp[1] = {A[1]-A[0]: 2}\n        for k in range(2, n):\n            for i in range(k):\n                diff = A[k] - A[i]\n                if diff in dp[i]:\n                    dp[k][diff] = dp[i][diff] + 1\n                else:\n                    dp[k][diff] = 2\n        return max(max(item.values()) for item in dp if item)", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            doneself = False\n            for lval in A[:i]:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                if nextval == val:\n                    if doneself:\n                        continue\n                    doneself = True\n                if diff in tex:\n                    c = tex[diff] + 1\n                else:\n                    c = 3\n                if c > res:\n                    res = c\n                toextend[nextval][diff] = c\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        from collections import defaultdict\n\n        d = [{} for _ in range(len(A))]\n        res = 2\n\n        for i, x in enumerate(A):\n            for j in range(i):\n                diff = x - A[j]\n                if diff in d[j]:\n                    d[i][diff] = max(d[j][diff] + 1, d[i][diff]) if diff in d[i] else d[j][diff] + 1\n                    d[j].pop(diff)\n\n                    res = max(res, d[i][diff])\n                    \n                else:\n                    d[i][diff] = 2\n\n                \n        return res\n\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        a = A\n        n = len(a)\n        \n        count = [{} for i in range(n)]\n        \n        for i in range(1,n):\n            for j in range(i):\n                diff = a[i] - a[j]\n                \n                if diff in count[j]:\n                    count[i][diff] = 1 + count[j][diff]\n                else:\n                    count[i][diff] = 1\n                \n        max_val = 0\n        for item in count:\n            if item:\n                max_val = max(max_val, max(item.values()))\n        \n        return max_val+1", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        length = len(A)\n        onleft = set()\n        onright = [0 for _ in range(501)]\n        toextend = [{} for _ in range(501)]\n        res = 2\n        for v in A:\n            onright[v] += 1\n        for i in range(0, length):\n            val = A[i]\n            tex = toextend[val]\n            onright[val] -= 1\n            for lval in onleft:\n                diff = val - lval\n                nextval = val + diff\n                if nextval > 500 or nextval < 0 or onright[nextval] == 0:\n                    continue\n                c = tex.get(diff, 2) + 1\n                if c > res:\n                    res = c\n                else:\n                    ending = (res - c) * diff + nextval\n                    if ending > 500 or ending < 0 or onright[ending] == 0:\n                        continue\n                toextend[nextval][diff] = c\n            onleft.add(val)\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        n = len(A)\n        dp = [[2] * n for i in range(n)]\n        index = [-1] * 501\n        res = 2\n        for i in range(n):\n            for j in range(i+1, n):\n                first = 2 * A[i] - A[j]\n                if first < 0 or first >= 500 or index[first] == -1:\n                    continue\n                dp[i][j] = dp[index[first]][i] + 1\n                res = max(res, dp[i][j])\n            index[A[i]] = i\n        return res", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        size = len(A)\n        if size <= 1:\n            return size\n        \n        nums = [{} for _ in range(size)]\n        for i in range(1, size):\n            for j in range(0, i):\n                diff = A[i] - A[j]\n                if diff in nums[j]:\n                    nums[i][diff] = nums[j][diff]+1\n                else:\n                    nums[i][diff] = 2\n            \n        max_num = 0\n        for i in range(1, size):\n            max_num = max(max_num, max(nums[i].values()))\n        return max_num\n            \n", "class Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for endi, endv in enumerate(A[1:], start = 1):\n            for starti, startv in enumerate(A[:endi]):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        dp = dict()\n        for starti, startv in enumerate(A):\n            for endi, endv in enumerate(A[starti+1:], start = starti+1):\n                diff = endv - startv\n                if (starti, diff) in dp:\n                    dp[(endi, diff)] = dp[(starti, diff)] + 1\n                else:\n                    dp[(endi, diff)] = 2\n        return max(dp.values())\n    \nclass Solution:\n    def longestArithSeqLength(self, A):\n        N = len(A)\n        dp = [{0:1} for _ in range(N)]\n        for end in range(1, N):\n            for start in range(end):\n                diff = A[end] - A[start]\n                if diff in dp[start]:\n                    dp[end][diff] = dp[start][diff] + 1\n                else:\n                    dp[end][diff] = 2\n        return max(max(dp[end].values()) for end in range(1, N))\n", "from collections import defaultdict, Counter\nclass Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:        \n        dp = defaultdict(lambda: defaultdict(lambda: 1))\n        best = 0\n        for i in range(len(A)):\n            num = A[i]\n\n            for prev_num in list(dp.keys()):\n                step = num - prev_num\n                dp[num][step] = max(dp[prev_num][step] + 1, dp[num][step])\n                best = max(best, dp[num][step])\n            dp[num][0] = max(dp[num][0], 1)\n            best = max(dp[num][0], best)\n        return best\n                \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        dp = {}\n        for i, a2 in enumerate(A[1:], start=1):\n            for j, a1 in enumerate(A[:i]):\n                d = a2 - a1\n                if (j, d) in dp:\n                    dp[i, d] = dp[j, d] + 1\n                else:\n                    dp[i, d] = 2\n        return max(dp.values())\n    \n#         dp = {}\n#         for i, a2 in enumerate(A[1:], start=1):\n#             for j, a1 in enumerate(A[:i]):\n#                 d = a2 - a1\n#                 if (j, d) in dp:\n#                     dp[i, d] = dp[j, d] + 1\n#                 else:\n#                     dp[i, d] = 2\n#         return max(dp.values())\n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        result = 2\n        \n        L = len(A)\n        index = [-1] * 2001 \n        \n        dp = [[2] * L for _ in range(L)]\n\n        \n        for i in range(L - 1):\n            for j in range(i + 1, L):\n                prevVal = 2 * A[i] - A[j]\n                \n                if index[prevVal] == -1:\n                    continue\n                else:\n                    idx = index[prevVal]\n                    if idx == -1:\n                        dp[i][j] = 2\n                    else:\n                        dp[i][j] = dp[idx][i] + 1\n                        result = max(result, dp[i][j])\n            \n            index[A[i]] = i\n        \n        return result", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        a = len(A)\n        dp = [[0]*a for _ in range(a)] # dp array\n        #print(dp)\n        index = [-1]*20001#index array\n        maximum = 2\n        for i in range(0,a):\n            dp[i] = [2]*a\n            for j in range(i+1, a):\n                #print(\\\"A[i]\\\",A[i],\\\"A[j]\\\",A[j] )\n                first = A[i]*2-A[j]\n                #print(\\\"first\\\",first)\n                if first < 0 or index[first]==-1:\n                    continue\n                else:\n                    #print(\\\"index[first]\\\",index[first])\n                    #print(\\\"dp[index[first]][i]\\\",dp[index[first]][i])\n                    dp[i][j] = dp[index[first]][i]+1\n                    #print(\\\"dp[i][j]\\\",dp[i][j])\n                    maximum = max(maximum,dp[i][j] ) \n                    #print(\\\"max\\\", maximum)\n            #print(dp)\n            index[A[i]] = i\n        return maximum\n    \n    \n", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        \n        pos_diff_to_length = {}\n        result = 0\n        for i in range(1, len(A)):\n            for j in range(i):\n                if (j, A[i] - A[j]) in pos_diff_to_length:\n                    pos_diff_to_length[(i, A[i] - A[j])] = pos_diff_to_length[(j, A[i] - A[j])] + 1 \n                else:\n                    pos_diff_to_length[(i, A[i] - A[j])] = 2\n                result = max(result, pos_diff_to_length[(i, A[i] - A[j])])\n                \n        return result", "class Solution:\n    def longestArithSeqLength(self, A: List[int]) -> int:\n        DP = [0]*len(A)\n        for i in range(len(A)):\n            temp = {}\n            for j in range(i):\n                diff = A[i]-A[j]\n                temp[diff] = DP[j].get(diff, 0)+1\n            DP[i] = temp\n        return max(max(d.values()) for d in DP if d)+1"]