Find N Unique Integers Sum up to Zero - Problem
Given an integer n, return any array containing n unique integers such that they add up to 0.
The array should contain exactly n distinct integers, and their sum must equal zero. You can return any valid combination that satisfies these constraints.
Note: There are multiple valid answers for each input, so any correct solution will be accepted.
Input & Output
Example 1 — Even Number
$
Input:
n = 4
›
Output:
[1,-1,2,-2]
💡 Note:
Create 2 pairs of opposite numbers: (1,-1) and (2,-2). Sum: 1 + (-1) + 2 + (-2) = 0
Example 2 — Odd Number
$
Input:
n = 5
›
Output:
[0,1,-1,2,-2]
💡 Note:
Since n is odd, add 0 first, then create 2 pairs: (1,-1) and (2,-2). Sum: 0 + 1 + (-1) + 2 + (-2) = 0
Example 3 — Minimum Case
$
Input:
n = 1
›
Output:
[0]
💡 Note:
Only one unique integer that sums to zero is 0 itself
Constraints
- 1 ≤ n ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Given integer n (number of unique integers needed)
2
Process
Create symmetric pairs that cancel out, add 0 if n is odd
3
Output
Array of n unique integers with sum = 0
Key Takeaway
🎯 Key Insight: Symmetric pairs of numbers always cancel out, making zero sum guaranteed
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code