Sarcastic Summary: Why bother with finding unique integers that sum up to zero when you can just use a simple algorithm that involves symmetric pairs and zero? Here’s the trick: Instead of generating pairs like [1, -1, 2, -2, 0]
, simply add them up to get [0, 0, 0, 0, 0]
. And if n
is odd, you can even append the value 0
to the array to ensure that the sum remains zero. Sound too simple for you? Well, with this approach, you’ll get a list of any array containing n
unique integers such that they add up to 0
, and the solution only requires a few lines of code. Don’t believe me? Let’s take a look at a few examples:
-
Example 1: n = 5: [-7, -1, 1, 3, 4]
- -7, -1, 1,
1304. Find N Unique Integers Sum up to Zero
Difficulty: Easy
Topics:
Array
,Math
,Weekly Contest 169
Given an integer
n
, return any array containingn
unique integers such that they add up to0
.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
Hint:
- Return an array where the values are symmetric. (+x , -x).
- If n is odd, append value 0 in your returned array.
Solution:
We need to generate an array of
n
unique integers such that their sum is zero. The solution involves creating pairs of positive and negative integers that cancel each other out. Ifn
is odd, we include zero to ensure the sum remains zero.Approach
-
Check for Odd
n
: Ifn
is odd, we include zero in the result array. This is because zero does not affect the sum and helps in making the total number of elements odd. -
Generate Pairs: For the remaining even number of elements (if
n
was odd, we now haven-1
elements left), we generate pairs of positive and negative integers. For each integeri
from1
ton/2
, we addi
and-i
to the result array. These pairs sum to zero, ensuring the overall sum of the array is zero.
Let’s implement this solution in PHP: 1304. Find N Unique Integers Sum up to Zero
<?php /** * @param Integer $n * @return Integer[] */ function sumZero($n) { ... ... ... /** * go to ./solution.php */ } // Test cases print_r(sumZero(5)); // Example output: [1, -1, 2, -2, 0] print_r(sumZero(3)); // Example output: [1, -1, 0] print_r(sumZero(1)); // Example output: [0] ?>
Explanation:
-
Initialization: We start by initializing an empty array
$result
to store the integers. -
Handling Odd
n
: Ifn
is odd, we add0
to the result array and decrementn
by one. This ensures that the remainingn
is even, allowing us to form pairs of positive and negative integers. -
Generating Pairs: We loop from
1
ton/2
. In each iteration, we add the current integeri
and its negative counterpart-i
to the result array. These pairs cancel each other out, contributing zero to the total sum. -
Return Result: The resulting array contains
n
unique integers (including zero ifn
was initially odd) that sum to zero.
This approach efficiently generates the required array by leveraging symmetric pairs of integers and zero, ensuring correctness and simplicity. The time complexity is O(n), as we iterate through each element once, and the space complexity is O(n) to store the result array.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
- -7, -1, 1,