[LeetCode] Find N Unique Integers Sum up to Zero

1304. Find N Unique Integers Sum up to Zero

Given an integer n, return any array containing n unique integers such that they add up to 0.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
vector<int> sumZero(int n) {
vector<int> res;
for(int i = 0; i < n / 2; i++) {
res.push_back(i+1);
res.push_back(-i-1);
}
if(n & 1) res.push_back(0);
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/09/07/PS/LeetCode/find-n-unique-integers-sum-up-to-zero/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.