Skip to main content
Back to problems
#1304
Easy Algorithms

Find n unique integers sum up to zero

Array Math
78.5% acceptance
Feb 25, 2026
2484
620
Given an integer n, return any array containing n unique integers such that they add up to 0.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn sum_zero(n: i32) -> Vec<i32> {
    let mut res: Vec<i32> = (1..n).collect();
    let s: i32 = res.iter().sum();
    res.push(-s);
    res
  }
}