Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #User function Template for python3
- class Solution:
- def lengthOfLongestAP(self, A, n):
- B=[{} for i in range(n)]
- B[0][A[0]]=1
- for i in range(1,n):
- for j in range(i):
- d=A[i]-A[j]
- if d in B[i]:
- B[i][d]=max(B[i][d],1+B[j].get(d,1))
- else:
- B[i][d]=B[j].get(d,1)+1
- ans=[]
- for i in B:
- if i:
- ans.append(max(i.values()))
- else:
- ans.append(0)
- return max(ans)
- #{
- # Driver Code Starts
- #Initial Template for Python 3
- if __name__ == '__main__':
- tc = int(input())
- while tc > 0:
- n = int(input())
- A = list(map(int, input().split()))
- ob = Solution()
- ans = ob.lengthOfLongestAP(A, n)
- print(ans)
- tc -= 1
- # } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment