List Every Unique Triplet That Sums to Zero

Implement zeroSumTriplets

Given an integer array nums, find every combination of three different elements whose values add up to zero, and report them with no duplicate triplet appearing twice — a triplet is a duplicate of another if it has the same three values, regardless of which indices produced them. Since the answer is checked as a single flat list of numbers rather than a grouped structure, package your result this way: sort the three values inside each triplet from smallest to largest, then arrange the triplets themselves from smallest to largest (comparing their first value, then second, then third), and concatenate everything into one array. For example, triplets (2, -1, -1) and (0, -1, 1) would be reported together as [-1, -1, 2, -1, 0, 1].

Example 1:

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

Output: [-1,-1,2,-1,0,1]

Example 2:

Input: nums = [0,0,0]

Output: [0,0,0]

Example 3:

Input: nums = [1,2,-2,-1]

Output: []

+ 4 hidden test cases run on Submit.

Constraints:

  • 3 ≤ nums.length ≤ 3000
  • -10⁵ ≤ nums[i] ≤ 10⁵

nums =

[-1, 0, 1, 2, -1, -4]