1304. Find N Unique Integers Sum up to Zero LeetCode Solution

In this guide, you will get 1304. Find N Unique Integers Sum up to Zero LeetCode Solution with the best time and space complexity. The solution to Find N Unique Integers Sum up to Zero problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find N Unique Integers Sum up to Zero solution in C++
  4. Find N Unique Integers Sum up to Zero solution in Java
  5. Find N Unique Integers Sum up to Zero solution in Python
  6. Additional Resources
1304. Find N Unique Integers Sum up to Zero LeetCode Solution image

Problem Statement of Find N Unique Integers Sum up to Zero

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

Example 1:

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

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1304. Find N Unique Integers Sum up to Zero LeetCode Solution in C++

class Solution {
 public:
  vector<int> sumZero(int n) {
    vector<int> ans(n);

    for (int i = 0; i < n; ++i)
      ans[i] = i * 2 - n + 1;

    return ans;
  }
};
/* code provided by PROGIEZ */

1304. Find N Unique Integers Sum up to Zero LeetCode Solution in Java

class Solution {
  public int[] sumZero(int n) {
    int[] ans = new int[n];

    for (int i = 0; i < n; ++i)
      ans[i] = i * 2 - n + 1;

    return ans;
  }
}
// code provided by PROGIEZ

1304. Find N Unique Integers Sum up to Zero LeetCode Solution in Python

class Solution:
  def sumZero(self, n: int) -> list[int]:
    return list(range(1 - n, n, 2))
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.