LeetCode 15 3Sum - Why Naive Fails

The trap

Check all triplets (i,j,k)(i, j, k) with three nested loops. Compute each sum, collect those equal to zero.

With n=3000n = 3000, you have roughly (30003)4.5×109\binom{3000}{3} \approx 4.5 \times 10^9 triplets. That's far too slow.

Beyond speed, you'd generate duplicate triplets. For [-1, -1, 0, 1], the triplet [-1, 0, 1] appears twice using different -1 values.