← DiffPush

3Sum

DiffPush Tier Arrays · Hard O(n^2) · O(1) auxiliary (excluding the output)

Balancing Three Meter Channels to Net Zero

A substation reads three phase offsets per sample, recorded as signed integer corrections, and the operator must find every distinct triple of offsets that sums to exactly zero. Offsets come from different samples so the same value may be reused at most once per triple. The triples are reported as value sets, so permutations of the same three values count once.

Input: An array nums of n integers.

Output: All distinct triples of values from nums whose sum is zero, with no duplicate triples and order not significant.

Constraints

Examples

Example 1

Input: {"nums":[-1,0,1,2,-1,-4]}
Output: [[-1,-1,2],[-1,0,1]]
Only two distinct value sets net to zero, even though the repeated -1 sits at two different indices.

Example 2

Input: {"nums":[0,1,1]}
Output: []
No combination of three offsets cancels here, so the result is empty.

Solve this in your browser →

Also on LeetCode ↗