DDSA
Advertisement

1304. Find N Unique Integers Sum up to Zero

Time: O(n)
Space: O(n)

Approach

Fill pairs (i, -i) for i = 1..n/2; if n is odd, the last element stays 0.

1304.cs
C#
// Approach: Fill pairs (i, -i) for i = 1..n/2; if n is odd, the last element stays 0.
// Time: O(n) Space: O(n)

public class Solution
{
    public int[] SumZero(int n)
    {
        // Initialize result array with size n
        int[] result = new int[n];

        // Fill the array with pairs of positive and negative integers
        // We iterate from 1 to n/2 and place each number and its negative
        int index = 0;
        for (int i = 1; i <= n / 2; i++)
        {
            // Add positive number
            result[index++] = i;
            // Add corresponding negative number
            result[index++] = -i;
        }

        // If n is odd, the last element remains 0 (default value)
        // This ensures the sum is still 0

        return result;
    }
}
Advertisement
Was this solution helpful?