LeetCode Entry
1304. Find N Unique Integers Sum up to Zero
Any n uniq numbers with sum of 0
1304. Find N Unique Integers Sum up to Zero easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1105
Problem TLDR
Any n uniq numbers with sum of 0 #easy
Intuition
- fill symmetrical -i,i, then remove 0 if n is even
- derive the law
1-n+i*2(from lee) - fill range
2..nthen add-sumof that
Approach
- careful with even/odd
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(n)\)
Code
// 9ms
fun sumZero(n: Int) = (2..n)+-(2..n).sum()
// 0ms
fun sumZero(n: Int) = IntArray(n) {1-n+it*2}
// 0ms
pub fn sum_zero(n: i32) -> Vec<i32> {
(1..n).chain([(n-n*n)/2]).collect()
}
// 0ms
vector<int> sumZero(int n) {
vector<int> r(n); iota(begin(r),end(r),1);
r.back() = (n-n*n)/2; return r;
}
// 0ms
sumZero = lambda _,n:[*range(1-n,n,2)]