Ctrl K

Neighbour Sums Around a Circle

Easy
Players sit around a round table, each holding a card with a number; the numbers are listed clockwise in ring, so the first player comes right after the last. The host calls out a number span, and everyone rewrites their card at the same moment: - if span > 0, a player writes the sum of the numbers held by the next span players clockwise; - if span < 0, the sum of the |span| players before them; - if span = 0, they write 0. Everyone works from the old numbers. Return the new numbers in the same order.
Example 1
ring=[3, 1, 4, 2], span=2 → [5, 6, 5, 4]

Each number becomes the sum of the next two around the circle: [1+4, 4+2, 2+3, 3+1] = [5, 6, 5, 4].

Example 2
ring=[3, 1, 4, 2], span=-1 → [2, 3, 1, 4]

Each number takes the one before it; for the first element, that is the last one.

Example 3
ring=[6, 9], span=0 → [0, 0]

With span = 0 every number turns into 0.

Constraints
  • 1 ≤ len(ring) ≤ 100
  • 1 ≤ ring[i] ≤ 100
  • -(len(ring) - 1) ≤ span ≤ len(ring) - 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRepeat Within Reach EasyValues One Apart EasyBest Window Average EasyStep Streak Score EasyTightest Group of Scores Easy
loading Python…
Input
ring=[3, 1, 4, 2], span=2
Expected
[5, 6, 5, 4]

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.