Created
September 7, 2025 16:58
-
-
Save tatsuyax25/51374a508142eeb5ec9555be2fed7798 to your computer and use it in GitHub Desktop.
Given an integer n, return any array containing n unique integers such that they add up to 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} n | |
| * @return {number[]} | |
| */ | |
| var sumZero = function(n) { | |
| const result = []; | |
| // Add symmetric pairs: [-1, 1], [-2, 2], ... | |
| for (let i = 1; i <= Math.floor(n / 2); i++) { | |
| result.push(-i, i) | |
| } | |
| // If n is odd, include 0 to balance the sum | |
| if (n % 2 !== 0) { | |
| result.push(0); | |
| } | |
| return result; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment