FunctionRacerProblemsRace

Ring Neighbor Sums

EasyArraysPython, JavaScript, TypeScript, Java, C++

The items of nums sit in a ring, so the last item is next to the first. Build a new list where item i is the sum of the two items beside position i in the ring: the one just before it and the one just after it, not counting the item itself.

With two items, each item's two neighbors are both the other item, so the sum is twice the other item. With one item, both neighbors are the item itself. Return the new list; nums is never empty.

Example 1

nums [1,2,3,4]
returns [6,4,6,4]

Position 0 sits between 4 and 2, position 1 between 1 and 3, position 2 between 2 and 4, position 3 between 3 and 1.

Example 2

nums [5,7]
returns [14,10]

Each of the two items has the other on both sides.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems