We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
sum() sums up all of the elements that matches the condition, (i+j)%k==0, and the elements are gathered from pair (x,i).
enumerate(a) returns multiple pairs, (x, i), where x is the index of the element, and i is the element value itself, for each element in the array a. That also means j is the element in the array a where the index is x+1 or to the right of a[x] by 1. In short, i is a[x] and j is a[x+1].
So, you have the array elements, i, and j. Given that when iterating through the array a, the (i+j)%k==0 then becomes something similar to the following sequence, due to True being evaluated as 1:
0, 0, 1, 0, 0, 0, 1, 0, ...
Which when calling on sum(), will give you the total count of those that satisfied the condition (i+j)%k==0.
res = 0 + 0 + 1 + 0 + 0 + 0 + 1 + 0 + ...
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Divisible Sum Pairs
You are viewing a single comment's thread. Return to all comments →
res
is the result.sum()
sums up all of the elements that matches the condition,(i+j)%k==0
, and the elements are gathered from pair(x,i)
.enumerate(a)
returns multiple pairs,(x, i)
, wherex
is the index of the element, andi
is the element value itself, for each element in the arraya
. That also meansj
is the element in the arraya
where the index isx+1
orto the right of a[x] by 1
. In short,i
isa[x]
andj
isa[x+1]
.So, you have the array elements,
i
, andj
. Given that when iterating through the arraya
, the(i+j)%k==0
then becomes something similar to the following sequence, due toTrue
being evaluated as1
:0, 0, 1, 0, 0, 0, 1, 0, ...
Which when calling on
sum()
, will give you the total count of those that satisfied the condition(i+j)%k==0
.res = 0 + 0 + 1 + 0 + 0 + 0 + 1 + 0 + ...