Unique Integers That Sum to Zero
Problem statement
Given an integer n, return any array of exactly n distinct integers whose sum is 0.
The values may appear in any order. Every returned value must fit a signed 32-bit integer. Any array that meets these rules is accepted; it does not need to match the examples.
Function
sumZero(n: int) → int[]Examples
Example 1
n = 5return = [-1,1,-2,2,0]These five integers are distinct. The pairs -1 + 1 and -2 + 2 each sum to 0, and the remaining value is 0.
Example 2
n = 4return = [-1,1,-2,2]There are four distinct integers, and their sum is -1 + 1 - 2 + 2 = 0.
Example 3
n = 1return = [0]A one-element array must contain 0 to have sum 0.
Constraints
1 <= n <= 100.