Skip to content
← All posts

Find N Unique Integers Sum up to Zero: Symmetric Pairs

4 min read
leetcodeproblemeasyarraysmath

Given an integer n, return any array containing n unique integers that sum to 0. There are multiple valid answers for each n, and you can return any one of them.

This is LeetCode 1304: Find N Unique Integers Sum up to Zero.

n = 5: result = [-2, -1, 0, 1, 2]-2-10center1+2+(-2) + 2 = 0(-1) + 1 = 0
Each positive number is paired with its negative. For odd n, a zero sits in the center. Every pair sums to zero, so the total is zero.

Why this problem matters

This problem teaches you to think in terms of mathematical symmetry before reaching for loops and data structures. The moment you realize that every number has an opposite that cancels it, the solution writes itself. That kind of observation, spotting structure before writing code, is exactly what interviewers test for in design-flavored array problems.

It also shows up as a building block whenever you need to construct arrays with specific sum constraints. Problems like partition equal subset sum and combination sum rely on the same idea of balancing positive and negative contributions.

The key insight

For any n, you can build the result from symmetric pairs. Loop i from 1 to n // 2, and for each i, add both i and -i to your array. Each pair sums to zero. If n is odd, you have one slot left over, so you append 0, which does not change the sum.

That is the entire algorithm. No sorting, no hash maps, no edge-case gymnastics. Just pairs of opposites and an optional zero.

The solution

def sum_zero(n):
    result = []
    for i in range(1, n // 2 + 1):
        result.append(i)
        result.append(-i)
    if n % 2 == 1:
        result.append(0)
    return result

Notice there is no need to track sums or use extra data structures. The symmetry guarantees correctness by construction. Every positive value has its negative twin in the array, and 0 is its own inverse.

Visual walkthrough

Let's trace through the algorithm for n = 5 step by step.

Step 1: Generate symmetric pairs

Loop i from 1 to n // 2. For each i, append both i and -i to the result. Each pair cancels out to zero.

i+i-iPair sum
11-10
22-20

After this step the result is [1, -1, 2, -2]. Total sum = 0.

Step 2: Handle odd n by adding 0

If n is odd, we have placed n-1 elements so far (all in pairs). Append 0 to fill the last slot without changing the sum.

ElementValueRunning sum
pair 1 (+)11
pair 1 (-)-10
pair 2 (+)22
pair 2 (-)-20
center00

Final result for n = 5: [1, -1, 2, -2, 0]. Sum = 0.

The loop runs n // 2 = 2 times, producing four elements in two pairs. Since 5 is odd, we tack on a 0 at the end. The sum of the entire array is 1 + (-1) + 2 + (-2) + 0 = 0.

Complexity analysis

MetricValue
TimeO(n)
SpaceO(n)
Pairs generatedn // 2

The loop runs n // 2 times and each iteration does constant work. The output array holds exactly n elements. There is no auxiliary data structure beyond the result itself.

The building blocks

1. Symmetric pair generation

The core technique here is generating matched pairs of +i and -i. This pattern appears whenever you need to construct a collection with a target sum of zero (or any target, by adjusting one element). It is a form of constructive proof: instead of searching for a valid array, you build one that is correct by design.

for i in range(1, count + 1):
    result.append(i)
    result.append(-i)

2. Parity-based adjustment

Checking whether n is odd and adding a zero is a common micro-pattern. Many array construction problems require you to handle even and odd cases differently. The idea is always the same: handle the "bulk" with a uniform rule, then fix up the remainder.

if n % 2 == 1:
    result.append(0)

These two pieces combine here in the simplest way possible, but the same thinking applies to harder construction problems where you must assemble arrays with specific sum, XOR, or divisibility constraints.

Edge cases

  • n = 1: the loop does not run (n // 2 = 0), and since 1 is odd, you append 0. Result: [0].
  • n = 2: one pair is generated. Result: [1, -1]. No zero needed.
  • Large n: the algorithm scales linearly. For n = 10000, you get 5000 pairs. No performance concerns.
  • All valid outputs accepted: the problem accepts any valid array, so [-2, -1, 0, 1, 2] and [1, -1, 2, -2, 0] are both correct for n = 5.

From understanding to recall

This problem is simple enough that you can understand the solution in a minute. But can you reproduce it from scratch on a blank screen a week from now? The trap with easy problems is that you read the answer, think "obvious," and move on without ever writing it yourself. Then in an interview, you hesitate on whether the loop should start at 0 or 1, or whether the zero goes in for even or odd n. Practice writing the solution from memory. Space out your reviews. That is how the pattern sticks.

Related posts

  • Two Sum - Another foundational array problem where you look for pairs with a target sum
  • 3Sum - Extends the pair idea to triplets that sum to zero
  • Move Zeroes - A different angle on arrays and zero handling with two pointers

The symmetric pairs approach to this problem is a reminder that the best solutions often come from mathematical structure, not clever algorithms. When you see a construction problem, ask yourself: "Is there a shape that makes the answer obvious?" Here, symmetry around zero is that shape. One loop, one parity check, done.