Sam and substrings Discussions | Algorithms | HackerRank
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.
I am not sure if others used the same logic, but the idea is that for the current sum, we multiply the previous sum by 10 and then add "extra". This "extra" grows each iteration, specially by the 1-based index of the current digit times the current digit value. For example, if the current digit is 3 and the 1-based index of this digit is 4, then the "extra" would increase by 12. So at any given digit, the sum is: currentSum = previousSum*10 + (previousExtra + (index * currentDigit)).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sam and substrings
You are viewing a single comment's thread. Return to all comments →
I am not sure if others used the same logic, but the idea is that for the current sum, we multiply the previous sum by 10 and then add "extra". This "extra" grows each iteration, specially by the 1-based index of the current digit times the current digit value. For example, if the current digit is 3 and the 1-based index of this digit is 4, then the "extra" would increase by 12. So at any given digit, the sum is: currentSum = previousSum*10 + (previousExtra + (index * currentDigit)).