Distinct Triplets That Sum to Zero
Problem statement
Given an integer array nums, find every group of three values, taken from three different positions, whose sum is 0.
Groups are compared by their values, not their positions: list each different group once. Write each group in increasing order, and sort the list of groups in increasing order (by the first value, then the second, then the third). Return an empty list if there is no such group.
Function
zeroSumTriplets(nums: int[]) → int[][]Examples
Example 1
nums = [3,-2,-1,0,2,-3]return = [[-3,0,3],[-2,-1,3],[-2,0,2]]Three groups add up to 0: -3 + 0 + 3, -2 + -1 + 3 and -2 + 0 + 2. Sorted, they are [-3, 0, 3], [-2, -1, 3], [-2, 0, 2]. No other three values add up to 0 (for example -3 + 2 + 0 = -1).
Example 2
nums = [4,-2,-2,-2,5]return = [[-2,-2,4]]The value -2 appears three times, so [-2, -2, 4] can be formed (using two of the -2s), but it is listed once. No group with 5 works: the other two values would have to add up to -5, and the smallest pair is -2 + -2 = -4.
Example 3
nums = [1,2,3,-10]return = []The possible groups add up to 6, -7, -6 and -5; none is 0, so the answer is an empty list.
Constraints
3 <= nums.length <= 3000-10^5 <= nums[i] <= 10^5