Replace Circular Entries with Neighbor Sums. For every position in circular array code, compute a replacement from the original array. If k is positive, sum the next k entries; if negative, sum the preceding -k entries; if zero, use zero.
### Problem
For every position in circular array `code`, compute a replacement from the original array. If `k` is positive, sum the next k entries; if negative, sum the preceding -k entries; if zero, use zero. Return all replacements in their original position order.
```cpp
class Solution {
public:
vector<int> decrypt(vector<int>& code, int k);
};
```
### Examples
**Example 1:**
```text
code = [3,1,6,2], k = 2
Output: [7,8,5,4]
Explanation: The last two positions wrap around to include the beginning.
```
**Example 2:**
```text
code = [4,2,7], k = -1
Output: [7,4,2]
Explanation: Each replacement is its predecessor in the circular order.
```
### Constraints
- `n == code.length`, with `1 <= n <= 100`.
- `1 <= code[i] <= 100`.
- `-(n-1) <= k <= n-1`.
### Clarifying Questions
All replacements are simultaneous: newly computed values must never supply a later sum. The current entry is not among its own neighbors.
```hint Inspect the structure
Which original entries belong to each sum for positive, negative, or zero `k`?
```
### Reasoning checkpoints
- Handle the sign of k and circular wrapping.
- Read only original entries when creating replacements.
### Optional extensions
How could a sliding sum reduce the work from O(n*|k|) to O(n)?
Overview: Replace Circular Entries with Neighbor Sums. For every position in circular array code, compute a replacement from the original array. If k is positive, sum the next k entries; if negative, sum the preceding -k entries; if zero, use zero.
For every position in circular array code, compute a replacement from the original array. If k is positive, sum the next k entries; if negative, sum the preceding -k entries; if zero, use zero. Return all replacements in their original position order.
class Solution {
public:
vector<int> decrypt(vector<int>& code, int k);
};
Examples
Example 1:
code = [3,1,6,2], k = 2
Output: [7,8,5,4]
Explanation: The last two positions wrap around to include the beginning.
Example 2:
code = [4,2,7], k = -1
Output: [7,4,2]
Explanation: Each replacement is its predecessor in the circular order.
Constraints
n == code.length
, with
1 <= n <= 100
.
1 <= code[i] <= 100
.
-(n-1) <= k <= n-1
.
Clarifying Questions Guidance
All replacements are simultaneous: newly computed values must never supply a later sum. The current entry is not among its own neighbors.
Reasoning checkpoints
Handle the sign of k and circular wrapping.
Read only original entries when creating replacements.
Optional extensions
How could a sliding sum reduce the work from O(n*|k|) to O(n)?