Show problems as
Classic problem #1304EasyArrayMath

Find N Unique Integers Sum up to Zero

Real-world version: Weights That Balance Around Nothing
Tracked in this browser only
Read Find N Unique Integers Sum up to Zero on LeetCode(opens in a new tab)

The 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.

Python
def sum_zero(n: int) -> list[int]:
Java
public int[] sumZero(int n)