samedi 14 mars 2020

What's the problem in my solution of Pairs having similar elements? I have attached my code below. It shows error that list index is out of range

''' #for finding transivity element #for finding similar elements def SimilarElementsPairs (A,N): count=0 L=list(A) L.sort() for i in range(0,N):

            if (L[i+1]-L[i])==1:
                count+=1
            elif (L[i+2]-L[i])==1:
                count+=1
        for j in range(0,N):

            if (L[j+1]-L[j])==1 and (L[j+2]-L[j+1])==1:
                count+=1
            elif (L[j+2]-L[j])==1 and (L[j+1]-L[j])==1:
                count+=1
            elif (L[j+2]-L[j])==1 and (L[j+2]-L[j+1])==1:
                count+=1
        return(count)


    N = int(input())
    A = map(int,input().split())
    out_ = SimilarElementsPairs(A,N)
    print (out_)

'''

Aucun commentaire:

Enregistrer un commentaire