human
Longest subsequence forming an Arithmetic Progression ( AP ) | Function that finds the longest arithmetic subsequence having the same absolute difference.
Stores the length of sequences having same difference.
Stores the resultant length.
Iterate over the array.
Update length of subsequence.
Return res.
.
Given array arr [ ].
Function Call"
Solution in Python:
assistant
#Here is code to longest subsequence forming an Arithmetic Progression ( AP ) and the other steps required.
 def lenghtOfLongestAP ( A , n ) :
	dp = { }
	res = 2
	for i in range ( n ) :
		for j in range ( i + 1 , n ) :
			d = A [ j ] - A [ i ]
			if d in dp :
				if i in dp [ d ] :
					dp [ d ] [ j ] = dp [ d ] [ i ] + 1
				else :
					dp [ d ] [ j ] = 2
			else :
				dp [ d ] = { }
				dp [ d ] [ j ] = 2
			if d in dp :
				if j in dp [ d ] :
					res = max ( res , dp [ d ] [ j ] )
	return res
/ * Driver Code * /
arr = [ 20 , 1 , 15 , 3 , 10 , 5 , 8 ]
N = len ( arr )
print ( lenghtOfLongestAP ( arr , N ) )