1304. Find N Unique Integers Sum up to Zero
Difficulty: Easy
Topics: Array, Math, Weekly Contest 169
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
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. If n is odd, we include zero to ensure the sum remains zero.
Approach
-
Check for Odd
n: Ifnis 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
nwas odd, we now haven-1elements left), we generate pairs of positive and negative integers. For each integerifrom1ton/2, we addiand-ito 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
$resultto store the integers. -
Handling Odd
n: Ifnis odd, we add0to the result array and decrementnby one. This ensures that the remainingnis even, allowing us to form pairs of positive and negative integers. -
Generating Pairs: We loop from
1ton/2. In each iteration, we add the current integeriand its negative counterpart-ito the result array. These pairs cancel each other out, contributing zero to the total sum. -
Return Result: The resulting array contains
nunique integers (including zero ifnwas 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:
Top comments (0)