It is required to count number of unordered pairs in an array of size n ,
l[i]+l[j] = 2*l[k] , where 1<=i< j<=n and 1<=k<= n
My Approach:
l = [1,3,5,4,2,5]
l.sort()
l2 = []
for i in l:
l2.append(2*i)
count = 0
for combo in combinations(l, 2):
if sum(combo) in l2:
count+=1
print(count) # count comes out to be 7
Please help in reducing Time Complexity O(n^2) to less if possible. Space Complexity is not a problem.