A ring of n signal beacons circles a harbor, numbered 0 through n-1 clockwise, with beacon n-1 followed again by beacon 0. Each beacon i currently broadcasts a raw strength code[i]. A single calibration key k recalculates every beacon's displayed strength at once, using only the original raw strengths (never the newly recalculated ones):
k > 0, beacon i's new strength becomes the sum of the raw strengths of the k beacons immediately clockwise of it (the next k beacons, wrapping around the ring as needed).k < 0, beacon i's new strength becomes the sum of the raw strengths of the |k| beacons immediately counter-clockwise of it (the previous |k| beacons, wrapping around the ring as needed).k = 0, every beacon's new strength becomes 0.Compute the new strength of every beacon.
Line 1: two integers n and k.
Line 2: n integers code[0] ... code[n-1], the raw strengths.
Print n space-separated integers: the recalculated strengths, in beacon order 0..n-1.
1 <= n <= 1001 <= code[i] <= 100-(n // 2) <= k <= n // 2 (integer division)Example 1
Input
6 2 2 4 6 8 10 12
Expected
10 14 18 22 14 6
Explanation
k=2>0, so each beacon sums the raw strengths of the next 2 beacons clockwise (wrapping at the ends). Beacon 0 sums beacons 1,2 (4+6=10); beacon 1 sums 2,3 (6+8=14); beacon 2 sums 3,4 (8+10=18); beacon 3 sums 4,5 (10+12=22); beacon 4 wraps to sum 5,0 (12+2=14); beacon 5 wraps to sum 0,1 (2+4=6). Output: 10 14 18 22 14 6.
Example 2
Input
4 -2 3 5 2 7
Expected
9 10 8 7
Explanation
k=-2<0, so each beacon sums the raw strengths of the previous 2 beacons counter-clockwise (wrapping at the ends). Beacon 0 wraps to sum beacons 3,2 (7+2=9); beacon 1 sums beacons 0,3 (3+7=10); beacon 2 sums beacons 1,0 (5+3=8); beacon 3 sums beacons 2,1 (2+5=7). Output: 9 10 8 7.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →