Skip to main content
Back to problems
Leetcode
Easy
Arrays
Math
Find N Unique Integers Sum Up To Zero

Construct any array of length n containing distinct integers whose sum is exactly 0.

Acceptance 0%
Problem Statement

Problem

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.

Input Format

  • A single integer n.

Output Format

  • Return or print an array of n unique integers whose total sum is 0.

Constraints

  • 1 <= n <= 1000
  • All integers in the answer must be distinct.
  • The sum of all integers in the answer must be 0.

Hints

  • Think about numbers in pairs like x and -x.
  • If n is odd, one value can be 0 and the remaining values can still be paired.

Input Format

  • Integer n.

Output Format

  • An array of n distinct integers with sum 0.

Constraints

  • 1 <= n <= 1000
  • All elements must be unique.
  • Sum of elements must be 0.
Examples
Sample cases returned by the problem API.

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

Unlock deeper context for this problem

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.