LeetCode Daily 07/09/2025 - 1304. Find N Unique Integers Sum up to Zero
Problem Link
LeetCode 1304 - Find N Unique Integers Sum up to Zero
Difficulty: Easy
Topic: Array, Math
Daily Question: 7th September 2025
Video Explanation
Watch the complete solution walkthrough on my YouTube channel!
Intuition
When we want n unique integers that sum to zero, the first idea is symmetry.
Pairs like (x, -x) cancel each other out perfectly. So the solution is to build the array from opposite pairs, and if needed, include 0 as a neutral element.
Approach
- If
nis even: taken // 2pairs of opposite numbers, e.g.(1, -1), (2, -2), .... - If
nis odd: do the same forn // 2pairs and add a single0at the end. - The order does not matter, since the problem does not require sorting.
This guarantees:
- ✅ Uniqueness: Each number appears exactly once
- ✅ Correct count: Exactly
nelements - ✅ Sum equals zero: Opposite pairs cancel out
Complexity Analysis
- Time complexity:
O(n)- We construct the result with one loop up ton // 2. - Space complexity:
O(n)- For storing the resulting array of sizen.
Solution
1 | class Solution: |
Example Walkthrough
Example 1: n = 5
- Pairs:
(1, -1), (2, -2)→[1, -1, 2, -2] nis odd, add0→[1, -1, 2, -2, 0]- Sum:
1 + (-1) + 2 + (-2) + 0 = 0✅
Example 2: n = 4
- Pairs:
(1, -1), (2, -2)→[1, -1, 2, -2] - Sum:
1 + (-1) + 2 + (-2) = 0✅
Key Takeaways
- Symmetry approach is perfect for sum-to-zero problems
- Handle odd/even cases separately for completeness
- Simple mathematics often leads to elegant solutions
- Order independence simplifies the implementation
Part of my daily LeetCode journey! Follow along for more algorithm solutions and explanations.
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.