Microsoft Online Assessment (OA) - 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 unique_sum(n: int) -> List[int]:
4
-
    # WRITE YOUR BRILLIANT CODE HERE
4
+
    res = []
5
-
    return []
5
+
    for i in range(n):
6
+
        res.append(i * 2 - n + 1)
7
+
    return res
8
+
6
9
if __name__ == '__main__':
7
10
    n = int(input())
8
11
    res = unique_sum(n)
9
12
    print(' '.join(map(str, res)))