Find N Unique Integers Sum up to Zero
Real-world version: Weights That Balance Around NothingThe statement here is our own: the same inputs, outputs and technique.
Choose n different whole numbers, positive, negative or nothing at all, that add up to nothing.
Return them in increasing order. Where several choices work, return the one whose numbers are as large as they can be, compared from the front: its first number is the largest any valid choice can start with, then its second is the largest given that, and so on.
Examples
Example 1
- Input
- n = 4
- Output
- [-2, -1, 1, 2]
Two pairs of opposites, taken at the smallest sizes going, so one against minus one and two against minus two.
Example 2
- Input
- n = 7
- Output
- [-3, -2, -1, 0, 1, 2, 3]
Three pairs of opposites use six of the seven numbers, and the one left over has to be nothing, since everything else already cancels.
Example 3
- Input
- n = 2
- Output
- [-1, 1]
A single pair of opposites, and the smallest sizes going are one and minus one.
Constraints
- 1 <= n <= 1000
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def sum_zero(n: int) -> list[int]:public int[] sumZero(int n)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 14 test cases: 14 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's