Skip to content
RED SAGSkills

Zero-Sum Triples

Given an integer array nums, find every distinct triple of values [a, b, c] taken from three different positions such that a + b + c = 0.

Each triple must be sorted ascending (a <= b <= c), the list must contain no duplicate triples, and the list itself must be sorted lexicographically.

Example 1

Input: nums = [-1, 0, 1, 2, -1, -4]
Output: [[-1, -1, 2], [-1, 0, 1]]

Example 2

Input: nums = [0, 0, 0, 0]
Output: [[0, 0, 0]]

Constraints

  • 3 <= nums.length <= 3000
  • -10^5 <= nums[i] <= 10^5
Hint 1

Sort the array, then fix the first value of the triple.

Hint 2

Use two pointers on the rest to find pairs summing to the negative of the fixed value, skipping duplicates.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Game Developer, Automation Test Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.