I did the Divisible Sum Pairs drawback on HackerRank
enter: n is size of array ar, ok is an integer worth.
a pair is outline provided that ar[i]+ar[j] is dividable by ok the place i < j.
Drawback: discover and return pairs in array.
def divisibleSumPairs(n, ok, ar):
i=0 #pointer 1
j=1 #pointer 2 is quicker than p1
rely =0 #pointer three will solely increment 1 as soon as j = n-1 (second final ingredient)
pairs=0
whereas rely < n-1:
if (ar[i]+ar[j])%ok==0:
pairs+=1
j+=1
rely+=1
if rely==n-1:
i+=1
j=i+1
rely =i
print(pairs)
Code visualized
step 1:
i j
[a][b][c][e][f]
evaluate (i,j)
i j
[a][b][c][e][f]
evaluate (i,j)
i j
[a][b][c][e][f]
evaluate (i,j)
i j
[a][b][c][e][f]
evaluate (i,j)
i+=1
j=i+1
step2
i j
[a][b][c][e][f]
evaluate (i,j)
.
.
.
.
step n
i j
[a][b][c][e][f]
evaluate (i,j)
end
I’ve a few questions:
- Does this algorithm have a reputation?
- Is there different approach (possibly higher, quicker)?
- Is there some code I might have carried out higher/ in another way?
#some take a look at knowledge n=6 , ok=three ar =[1, 3, 2, 6, 1, 2]