1 / 3
Nov 2018

Greatings
my id =22647213
can anyone please tell why TLE(time limit exceeded)
def magic(b,diff):
count=0
for i in range(len(b)):
for j in range(i+1,len(b)):
if((b[i]-b[j])==diff):
count=count+1
# break
return count
n,diff=map(int,input().split())
a=list(map(int,input().split()[:n]))
b=sorted(a,reverse=True)
#print(‘sorted list’, b);
print(magic(b,diff))

  • created

    Nov '18
  • last reply

    Nov '18
  • 2

    replies

  • 921

    views

  • 2

    users

Why do you sort the values? How does that sorting help you with the algorithm you’ve used?

Can you think of a different algorithm where sorting the values does help you?