Ctrl K

Zero-Sum Triples

Medium
You are given an integer array values. Find every distinct triple of numbers that can be picked from three different positions of the array so that the three numbers add up to zero. Return each triple as a list of three numbers. Two triples made of the same numbers count as the same triple, even if they come from different positions, so each must appear in the answer only once. If no such triple exists, return an empty list. Neither the order of the triples nor the order of numbers inside a triple matters.
Example 1
values=[3, -6, 2, 1, 3, -4, 0] → [[-6, 3, 3], [-4, 1, 3]]

Two triples work: −6 + 3 + 3 = 0 (the array has two 3s at different positions) and −4 + 1 + 3 = 0. The triple −4 + 2 + 2 does not count, since there is only one 2.

Example 2
values=[5, 1, 2, 7] → []

All numbers are positive, so no triple sums to zero.

Example 3
values=[0, 0, 0, 0, 0] → [[0, 0, 0]]

There are many zeros, but the all-zero triple appears in the answer only once.

Constraints
  • 3 ≤ length of values ≤ 3000
  • -10⁵ ≤ values[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTriple Sum Nearest the Target MediumFour Numbers to a Target MediumFlags in Three Colors MediumStudios for Recording Sessions MediumTriples Under a Limit Medium
loading Python…
Input
values=[3, -6, 2, 1, 3, -4, 0]
Expected
[[-6, 3, 3], [-4, 1, 3]]

Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.