Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: Given an integer n, return any array containing n unique integers such that they add up to 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
5
3
1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1304: Find N Unique Integers Sum up to Zero
class Solution {
public int[] sumZero(int n) {
int[] ans = new int[n];
for (int i = 1, j = 0; i <= n / 2; ++i) {
ans[j++] = i;
ans[j++] = -i;
}
return ans;
}
}
// Accepted solution for LeetCode #1304: Find N Unique Integers Sum up to Zero
func sumZero(n int) []int {
ans := make([]int, n)
for i, j := 1, 0; i <= n/2; i, j = i+1, j+1 {
ans[j] = i
j++
ans[j] = -i
}
return ans
}
# Accepted solution for LeetCode #1304: Find N Unique Integers Sum up to Zero
class Solution:
def sumZero(self, n: int) -> List[int]:
ans = []
for i in range(n >> 1):
ans.append(i + 1)
ans.append(-(i + 1))
if n & 1:
ans.append(0)
return ans
// Accepted solution for LeetCode #1304: Find N Unique Integers Sum up to Zero
impl Solution {
pub fn sum_zero(n: i32) -> Vec<i32> {
let mut ans = vec![0; n as usize];
let mut j = 0;
for i in 1..=n / 2 {
ans[j] = i;
j += 1;
ans[j] = -i;
j += 1;
}
ans
}
}
// Accepted solution for LeetCode #1304: Find N Unique Integers Sum up to Zero
function sumZero(n: number): number[] {
const ans: number[] = Array(n).fill(0);
for (let i = 1, j = 0; i <= n / 2; ++i) {
ans[j++] = i;
ans[j++] = -i;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.