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

1from typing import List
2
3def unique_sum(n: int) -> List[int]:
4    res = []
5    for i in range(n):
6        res.append(i * 2 - n + 1)
7    return res
8
9if __name__ == '__main__':
10    n = int(input())
11    res = unique_sum(n)
12    print(' '.join(map(str, res)))
13