Find N Unique Integers Sum up to Zero (LeetCode ) Problem Solution.

Find N Unique Integers Sum up to Zero (LeetCode ) Problem Solution.

Example 1:
Input: n = 5
Output: [-7,-1,1,3,4]
Explanation: These arrays are also 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 <= 1000

Solution :-


class Solution:

    def sumZero(self, n: int) -> List[int]:

        ans=[]

        if n==0:
            return []
        elif n==1:
            return [0]
        else:
            if n%2==0:
                temp=n//2

                for i in range(1,temp+1):
                    ans.append(-i)
                    ans.append(i)

            else:
                temp=n//2

                for i in range(1,temp+1):
                    ans.append(-i)
                    ans.append(i)

                ans.append(0)


        return ans
Runtime: 28 ms, faster than 86.44% of Python3 online submissions for Find N Unique Integers Sum up to Zero.
Memory Usage: 13.9 MB, but 100.00% of Python3 online submissions for Find N Unique Integers Sum up to Zero.