Construct any array of length n containing distinct integers whose sum is exactly 0.
Given an integer n, build an array of exactly n distinct integers such that the sum of all elements is 0.
Any valid answer is accepted. There may be multiple correct outputs.
A simple construction is to pair positive and negative numbers so they cancel out, and include 0 when needed.
n.n unique integers whose total sum is 0.1 <= n <= 10000.x and -x.n is odd, one value can be 0 and the remaining values can still be paired.n.n distinct integers with sum 0.1 <= n <= 10000.Example 1
Input
n = 5
Output
[-2,-1,0,1,2]
Explanation
All values are distinct and their sum is -2 + (-1) + 0 + 1 + 2 = 0.
Example 2
Input
n = 4
Output
[-2,-1,1,2]
Explanation
The numbers are unique and cancel out pairwise to make the sum 0.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.