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.
Uhm, let see, I'm guessing you're having trouble seeing how it manages to comply with the i < j rule. So, what does the rule really mean? It's only telling you that a value a[i] should not be paired with itself, and a tuple a[i],a[j] should not be considered twice so.. a[j],a[i] is not considered.
You can think of this solution of only running forward, so a number a[i] appears always before a number a[j]. As it runs forward, for each number it looks for previous occurrences of its complement (it looks for the i's for a particular j).
Say for instance if the divisor is 3, you could have this sequence:
1, 2, 1
The first is not matched with anything.
The second is matched with the first. Although the 2nd could be matched with the 3rd, the algorithm isn't yet aware of the 3rd value...
When the third value is read, it will be matched against the 2nd. Effectively matching the 2nd with the 3rd (remember a[i][j] and a[j][i] should only be counted once ;)
I'm no algorithm guru, I had never seen this problem before... just started exercising for fun. But from my perspective there's no golden rule here, I don't see a generalization of this that's particularly valuable.
If the intention is learning, I would recommend you to focus your energy in understanding more generalized forms of algorithms... these here are mostly "quizes", perhaps try an algorithms book to find generalized sets of problems and solutions.
Also, many interesting real life problems are more about the data structures used and then the algorith comes natural to the data structure itself, so perhaps a focus on data structures is worth your while too...
Of course, these quizes have a "fun" element, and exercising can really help in making the learning stick, so you could do both :P... just get the basics strong and then go for the corresponding quizes I guess.
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 →
Uhm, let see, I'm guessing you're having trouble seeing how it manages to comply with the
i < j
rule. So, what does the rule really mean? It's only telling you that a valuea[i]
should not be paired with itself, and a tuplea[i],a[j]
should not be considered twice so..a[j],a[i]
is not considered.You can think of this solution of only running forward, so a number
a[i]
appears always before a numbera[j]
. As it runs forward, for each number it looks for previous occurrences of its complement (it looks for the i's for a particular j).Say for instance if the divisor is 3, you could have this sequence:
1, 2, 1
a[i][j]
anda[j][i]
should only be counted once ;)I'm no algorithm guru, I had never seen this problem before... just started exercising for fun. But from my perspective there's no golden rule here, I don't see a generalization of this that's particularly valuable.
If the intention is learning, I would recommend you to focus your energy in understanding more generalized forms of algorithms... these here are mostly "quizes", perhaps try an algorithms book to find generalized sets of problems and solutions.
Also, many interesting real life problems are more about the data structures used and then the algorith comes natural to the data structure itself, so perhaps a focus on data structures is worth your while too...
Of course, these quizes have a "fun" element, and exercising can really help in making the learning stick, so you could do both :P... just get the basics strong and then go for the corresponding quizes I guess.