All problems
1048EasyArrayMath

Weights That Balance Around Nothing

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1304Find N Unique Integers Sum up to Zero

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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