Find N Unique Integers Sum up to Zero - Problem
You're tasked with creating a balanced array of unique integers! Given an integer n, you need to return an array containing exactly n unique integers that sum to zero.
This is a creative construction problem where you have complete freedom in choosing the integers, as long as they meet two requirements:
- All
nintegers must be unique (no duplicates) - Their sum must equal 0
Example: If n = 3, you could return [-1, 0, 1] since all numbers are unique and -1 + 0 + 1 = 0.
The beauty of this problem is that there are multiple valid solutions - you just need to find any valid array that satisfies the conditions!
Input & Output
example_1.py โ Basic Case
$
Input:
n = 5
โบ
Output:
[0, 1, -1, 2, -2]
๐ก Note:
We have 5 unique integers that sum to zero. Since n is odd, we include 0, then add two pairs (1,-1) and (2,-2). Total: 0 + 1 + (-1) + 2 + (-2) = 0
example_2.py โ Even Case
$
Input:
n = 4
โบ
Output:
[1, -1, 2, -2]
๐ก Note:
Since n is even, we don't need zero. We use two pairs: (1,-1) and (2,-2). Total: 1 + (-1) + 2 + (-2) = 0
example_3.py โ Edge Case
$
Input:
n = 1
โบ
Output:
[0]
๐ก Note:
The only way to have 1 unique integer that sums to zero is to use [0]
Constraints
- 1 โค n โค 1000
- The returned array must contain exactly n unique integers
- The sum of all integers must equal 0
- Any valid solution is acceptable
Visualization
Tap to expand
Understanding the Visualization
1
Identify the Pattern
For n elements, we need pairs that cancel out
2
Handle Odd Numbers
If n is odd, use 0 as the 'neutral' weight
3
Create Pairs
Add pairs (1,-1), (2,-2), etc. until we reach n elements
4
Verify Balance
All pairs sum to 0, ensuring perfect balance
Key Takeaway
๐ฏ Key Insight: Instead of searching for a solution, we construct one systematically using the mathematical property that opposite numbers cancel out!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code