Skip to content

Commit adb1a49

Browse files
authored
Create Find N Unique Integers Sum up to Zero.py
1 parent 46f4cfe commit adb1a49

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
3+
Given an integer n, return any array containing n unique integers such that they add up to 0.
4+
Given an integer n, return any array containing n unique integers such that they add up to 0.
5+
6+
7+
8+
Example 1:
9+
10+
Input: n = 5
11+
Output: [-7,-1,1,3,4]
12+
Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].
13+
Example 2:
14+
15+
Input: n = 3
16+
Output: [-1,0,1]
17+
Example 3:
18+
19+
Input: n = 1
20+
Output: [0]
21+
22+
23+
Constraints:
24+
25+
1 <= n <= 1000
26+
27+
"""
28+
class Solution:
29+
def sumZero(self, n: int) -> List[int]:
30+
if n == 1:
31+
return [0]
32+
else:
33+
r = []
34+
if n %2 !=0:
35+
r.append(0)
36+
37+
i = 1
38+
while len(r)<n:
39+
r.append(0-i)
40+
r.append(i)
41+
i+=1
42+
return r
43+
44+

0 commit comments

Comments
 (0)