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
Find N Unique Integers Sum up to Zero: n=4 ExampleInput: n = 4n = 4Process: Create Symmetric Pairs1-12-2Pair 1: Sum = 0Pair 2: Sum = 0Output: Array of 4 unique integers1-12-2Total Sum: 1 + (-1) + 2 + (-2) = 0 ✓
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
Asked in
Google 15 Microsoft 12 Amazon 8
33.4K Views
Medium Frequency
~8 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen