NEED HELP getting TLE every time

why my solution is getting TLE
question link: CodeChef: Practical coding for everyone
my solution:

`count = 0
 arr = list(map(int, input().split()))
 n = arr[0]
 l = []
 height = arr[1:]
 height.sort()
 z = []
 if n <= 2:
     print(2)
     exit()
for i in range(n):
    for h in range(i+1, n):
        z.append(height[h]-height[i])

for m in set(z):
    l.append(z.count(m))
print(max(l))`

The TLE has been removed. But you have to evaluate why this algorithm gives wrong answer.

for m in set(z):

The time complexity of this step should be close to n^2.
Then complexity of count is n.
Complexity of program O(n^3)

count = 0
arr = list(map(int, input().split()))
n = arr[0]
l = []
height = arr[1:]
height.sort()
z = []
if n <= 2:
 print(2)
 exit()
for i in range(n):
    for h in range(i+1, n):
        z.append(height[h]-height[i])

mp = {}

for m in z:
	if m not in mp:
		mp[m] = 0
	mp[m] += 1
	
print(max(mp.values()))
1 Like