Find N Unique Integers Sum up to Zero

Easy
489.

Find N Unique Integers Sum up to Zero

Find N Unique Integers Sum up to Zero is an easy coding problem on arrays and math, asked in Amazon and Adobe interviews.

Given an integer n, return an array of n unique integers that sum to 0.

Many such arrays exist; return the one whose values are packed as tightly around zero as possible, listed in strictly increasing order. Concretely: for odd n that is [-(n-1)/2, …, 0, …, (n-1)/2], and for even n it is the same range with 0 removed.

Example 1

Input
n = 5
Output
[-2,-1,0,1,2]

Example 2

Input
n = 4
Output
[-2,-1,1,2]
Explanation
Zero is dropped so the count stays even and the values stay unique.

Example 3

Input
n = 1
Output
[0]

Constraints

  • 1 <= n <= 1000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} n
 * @return {number[]}
 */
var sumZero = function(n) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Find N Unique Integers Sum up to Zero — Problem & Solution

Find N Unique Integers Sum up to Zero solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Find N Unique Integers Sum up to Zero is an easy coding problem on arrays and math, asked in Amazon and Adobe interviews.

  • Difficulty: Easy
  • Topics: Arrays, Math
  • Asked at: Amazon, Adobe
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

Given an integer n, return an array of n unique integers that sum to 0.

Many such arrays exist; return the one whose values are packed as tightly around zero as possible, listed in strictly increasing order. Concretely: for odd n that is [-(n-1)/2, …, 0, …, (n-1)/2], and for even n it is the same range with 0 removed.

Example 1

Input: n = 5
Output: [-2,-1,0,1,2]

Example 2

Input: n = 4
Output: [-2,-1,1,2]
Explanation: Zero is dropped so the count stays even and the values stay unique.

Example 3

Input: n = 1
Output: [0]

Constraints

  • 1 <= n <= 1000

Hints for Find N Unique Integers Sum up to Zero

  1. Hint 1

    Pairs of the form (-k, k) cancel out, so the sum takes care of itself.

  2. Hint 2

    For odd n you have one element left over after pairing — make it 0.

  3. Hint 3

    For even n, skipping 0 keeps every value distinct while preserving the symmetry.

How to solve Find N Unique Integers Sum up to Zero

Symmetry does all the work: -k and +k cancel, so any set built from such pairs sums to zero. Only the parity of n decides whether a lone 0 is needed.

Approach

  1. If n is odd, emit every integer from -(n-1)/2 to (n-1)/2 — that is n values including 0.
  2. If n is even, emit every integer from -n/2 to n/2 but skip 0 — again n values.
  3. Both ranges are already in increasing order.

Why it works

Each range is symmetric about zero, so the positives cancel the negatives exactly. The odd case has an odd count only because 0 is included; the even case has an even count only because 0 is excluded.

Complexity

  • Time — O(n)
  • Space — O(n) for the output

Pitfalls

  • Emitting 1, -1, 2, -2, … also sums to zero but is not in increasing order, so the exact judge rejects it.
  • Forgetting to skip 0 in the even case produces n + 1 values.
  • n = 1 must answer [0], which the odd branch already handles.

Reference solution

Python

from typing import List

def sumZero(n: int) -> List[int]:
    out = []
    if n % 2 == 1:
        half = (n - 1) // 2
        for i in range(-half, half + 1):
            out.append(i)
    else:
        half = n // 2
        for i in range(-half, half + 1):
            if i != 0:
                out.append(i)
    return out

Java

public static int[] sumZero(int n) {
    int[] out = new int[n];
    int idx = 0;
    if (n % 2 == 1) {
        int half = (n - 1) / 2;
        for (int i = -half; i <= half; i++) out[idx++] = i;
    } else {
        int half = n / 2;
        for (int i = -half; i <= half; i++) if (i != 0) out[idx++] = i;
    }
    return out;
}

C++

vector<int> sumZero(int n) {
    vector<int> out;
    if (n % 2 == 1) {
        int half = (n - 1) / 2;
        for (int i = -half; i <= half; i++) out.push_back(i);
    } else {
        int half = n / 2;
        for (int i = -half; i <= half; i++) if (i != 0) out.push_back(i);
    }
    return out;
}

JavaScript

var sumZero = function(n) {
    const out = [];
    if (n % 2 === 1) {
        const half = (n - 1) / 2;
        for (let i = -half; i <= half; i++) out.push(i);
    } else {
        const half = n / 2;
        for (let i = -half; i <= half; i++) if (i !== 0) out.push(i);
    }
    return out;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays