mediumTwo PointersPure DSA~28 min
Three Signal Sum Zero
An anomaly detector looks for triples of signed signal deltas that cancel out to zero — a sign of a balanced, non-anomalous window. Return all unique triples that sum to zero.
Problem
Given an integer array nums, return all unique triples [a, b, c] such that a + b + c = 0. The solution set must not contain duplicate triples.
Input
An array nums of length n (0 ≤ n ≤ 3000), values in [-10^5, 10^5].
Output
A list of unique triples summing to zero, in any order.
Constraints
- 0 ≤ n ≤ 3000
- No duplicate triples in the output
- Order within and across triples is unconstrained
Examples
Example 1
Input
nums = [-1, 0, 1, 2, -1, -4]
Output
[[-1, -1, 2], [-1, 0, 1]]
Two distinct triples sum to zero; the duplicate -1 is handled by skipping repeats.
Example 2
Input
nums = [0, 0, 0, 0]
Output
[[0, 0, 0]]
Only one unique zero triple despite many zeros.