Watch 10 video solutions for Find N Unique Integers Sum up to Zero, a easy level problem involving Array, Math. This walkthrough by codestorywithMIK has 4,694 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an integer n, return any array containing n unique integers such that they add up to 0.
Example 1:
Input: n = 5 Output: [-7,-1,1,3,4] Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].
Example 2:
Input: n = 3 Output: [-1,0,1]
Example 3:
Input: n = 1 Output: [0]
Constraints:
1 <= n <= 1000Problem Overview: You need to construct an array of n unique integers such that their total sum equals zero. The values can be any integers as long as they are distinct and the final sum is exactly 0. Because the order does not matter and many valid answers exist, the goal is simply to generate one correct combination efficiently.
Approach 1: Use Symmetric Pairing (O(n) time, O(n) space)
The simplest observation is that every positive number x cancels out with -x. Using this symmetry, you can construct pairs like (1, -1), (2, -2), and continue until you fill the array. Iterate from 1 to n/2 and append both i and -i to the result array. If n is odd, add a single 0 at the end since zero does not affect the sum and remains unique. The algorithm only requires a single pass to build the result, giving O(n) time complexity and O(n) space for the output array. This method works well because it directly exploits arithmetic symmetry instead of searching for combinations. Problems involving array construction often benefit from such mathematical observations. If you are practicing array manipulation patterns, see more examples under Array problems.
Approach 2: Consecutive Integers Strategy (O(n) time, O(n) space)
Another clean construction uses consecutive integers centered around zero. Generate numbers from -(n/2) up to n/2 while skipping zero when necessary to maintain uniqueness and the correct count. A common implementation builds values from 1 to n-1 and keeps a running sum, then adds the final value as -sum. Because the earlier values are distinct, the last number guarantees the total becomes zero. The loop runs once, so the time complexity is O(n) and the extra space used is O(n). This approach highlights a useful trick in Math-based problems: control the running sum and adjust the final element to satisfy the constraint.
Recommended for interviews: The symmetric pairing method is what most interviewers expect. It shows you immediately recognized the mathematical structure of the problem and produced a clean O(n) construction without unnecessary bookkeeping. The consecutive integer strategy is also valid and demonstrates comfort with array generation and running-sum reasoning. If you mention both approaches during discussion, it signals strong problem-solving depth even for an easy-level question.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Symmetric Pairing | O(n) | O(n) | Best general solution. Simple construction using positive and negative pairs. |
| Consecutive Integers Strategy | O(n) | O(n) | Useful when reasoning with running sums or constructing numbers sequentially. |