My approach is as follows
1. I am creating a dictionary for storing the differences between all pairs of numbers and the count
2. The key contains the difference and the value is a list. The first index of the list is the number of occurrences of the difference and the following indexes just represents the numbers which follow the Arithmetic Progression
I have written the following code for it
d = {}
for i in range(len(A)-1):
for j in range(i+1, len(A)):
if A[i]-A[j] in d.keys():
d[A[i]-A[j]][0] += 1
d[A[i]-A[j]].append(A[j])
else:
d[A[i]-A[j]] = [2, A[i], A[j]]
# Get the key,value pair having the max value
k,v = max(d.items(), key=lambda k: k[1])
print(v[0])
For instance, if the input is [20,1,15,3,10,5,8], my output is 4
However, my code is failing for the following input [83,20,17,43,52,78,68,45].
The expected outcome is 2 but I am getting 3. When I printed the contents of my dictionary, I found that in the dictionary, there were entries like,
-25: [3, 20, 45, 68], -26: [3, 17, 43, 78], -35: [3, 17, 52, 78]
I don't understand why they are present since, in the case of -25, the difference 68 and 45 is not 25 and I am making that check before adding the value to the dictionary. Can someone please point out the bug in my code?
My complete output is
{63: [2, 83, 20], 66: [2, 83, 17], 40: [2, 83, 43], 31: [2, 83, 52], 5: [2, 83, 78], 15: [2, 83, 68], 38: [2, 83, 45], 3: [2, 20, 17], -23: [2, 20, 43], -32: [2, 20, 52], -58: [2, 20, 78], -48: [2, 20, 68], -25: [3, 20, 45, 68], -26: [3, 17, 43, 78], -35: [3, 17, 52, 78], -61: [2, 17, 78], -51: [2, 17, 68], -28: [2, 17, 45], -9: [2, 43, 52], -2: [2, 43, 45], -16: [2, 52, 68], 7: [2, 52, 45], 10: [2, 78, 68], 33: [2, 78, 45], 23: [2, 68, 45]}

