DEV Community

Md. Rishat Talukder
Md. Rishat Talukder

Posted on

🧠 Solving LeetCode Until I Become Top 1% β€” Day `76`

πŸ”Ή Problem: 1304 Find N Unique Integers Sum up to Zero

Difficulty: #Easy
Tags: #Math, #Constructive


πŸ“ Problem Summary

Given an integer n, return an array of n unique integers such that they sum up to 0.


🧠 My Thought Process

  • Brute Force Idea:
    Generate random numbers and try to adjust until their sum is zero. This would be messy and inefficient.

  • Optimized Strategy:

    • Pair positive and negative integers: (1, -1), (2, -2), ...
    • If n is even, the pairs are enough.
    • If n is odd, just add 0 to balance everything out.
    • This guarantees uniqueness and ensures the sum is always 0.
  • Algorithm Used:

    Constructive Math (pairing positives and negatives).


βš™οΈ Code Implementation (Python)

class Solution:
    def sumZero(self, n: int) -> List[int]:
        ans = []
        for i in range(1, n // 2 + 1):
            ans.append(i)
            ans.append(-i)
        if n % 2 == 1:
            ans.append(0)
        return ans
Enter fullscreen mode Exit fullscreen mode

⏱️ Time & Space Complexity

  • Time: O(n) β€” we generate n elements.
  • Space: O(n) β€” storing the result array.

🧩 Key Takeaways

  • βœ… Learned how constructive math can directly give the answer without brute force.
  • πŸ’‘ The β€œpairing” trick is a powerful way to balance sums.
  • πŸ’­ If a problem asks for sums = 0 (or balanced counts), think about symmetry.

πŸ” Reflection (Self-Check)

  • [x] Could I solve this without help?
  • [x] Did I write code from scratch?
  • [x] Did I understand why it works?
  • [x] Will I be able to recall this in a week?

πŸš€ Progress Tracker

Metric Value
Day 76
Total Problems Solved 440
Confidence Today πŸ˜ƒ
Leetcode Rating 1530

Top comments (0)