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
Hint 1
Pairs of the form
(-k, k)cancel out, so the sum takes care of itself.Hint 2
For odd
nyou have one element left over after pairing — make it0.Hint 3
For even
n, skipping0keeps 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
- If
nis odd, emit every integer from-(n-1)/2to(n-1)/2— that isnvalues including0. - If
nis even, emit every integer from-n/2ton/2but skip0— againnvalues. - 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
0in the even case producesn + 1values. n = 1must 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 outJava
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