Largest Sequence Sum

Given a long value val, the largestSum method should return the largest sum calculated from 4 consecutive digits.

For example, if val is 1792511 the largestSum method would return 23 which is the sum of 7 + 9 + 2 + 5. Any other 4 digit sequence would add up to a value less than or equal to 23.

Let's look at the sum of all digits, starting with the first 4

$$ \Large \begin{align} 1 + 7 + 9 + 2 &= 19\\\\ 7 + 9 + 2 + 5 &= 23\\\\ 9 + 2 + 5 + 1 &= 17\\\\ 2 + 5 + 1 + 1 &= 9 \end{align} $$

var will be in the range [ 1000 - Long.MAX_VALUE ]

Note: The test for this challenge runs 100 random long values. If your code fails, it is unlikely for it to have the exact same number in the next set of tests.

Login / Register

To try your hand at solving this problem you'll need an account on CompSci.Rocks. Please either login or register to continue.

Once you've logged in you'll be able to enter code directly into this page to solve the problem.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *