Unique Integers That Sum Up To 0

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

Example 1:

Input:5

Output: [-4,-2,0,2,4]

Example 2:

Input:3

Output: [-2, 0, 2]

Example 1:

Input:1

Output: [0]

Try it yourself

Implementation

1
1
from typing import List
2
2
3
3
def uniqueSum(n) -> List[int]:
4
-
    # WRITE YOUR BRILLIANT CODE HERE
4
+
    ans = [0] * n
5
+
    index = 0
6
+
    while index < len(ans):
7
+
        ans[index] = index * 2 - n + 1;
8
+
        index += 1
9
+
    return ans
10
+
5
11
if __name__ == '__main__':
6
12
    res = uniqueSum(int(input()))
7
13
    print(' '.join(str(e) for e in res))