Unique Triplets Summing to Zero
Updated
Problem statement
You are given an integer array nums. Find every triplet of values [a, b, c] such that a + b + c == 0 and the three values come from three different positions in nums. A value can be used more than once in a triplet only if it appears that many times in nums.
Two triplets with the same three values count as the same triplet, even if they use different positions. Return each triplet once.
Output Order
- Write each triplet with its values in ascending order, so
a <= b <= c. - Sort the triplets by their first value, and break ties by their second value.
- If no triplet adds up to zero, return an empty list.
Function
zeroSumTriplets(nums: int[]) → int[][]Examples
Example 1
nums = [4,-2,-2,0,2,-4,1]return = [[-4,0,4],[-2,-2,4],[-2,0,2]]Sorted, the array is [-4, -2, -2, 0, 1, 2, 4]. Three groups of values add up to zero: -4 + 0 + 4, -2 + -2 + 4 (the array has two copies of -2), and -2 + 0 + 2. The group -2, 0, 2 can be made with either copy of -2, but it is listed once. Groups such as -4, 2, 2 or -2, 1, 1 would need a second 2 or a second 1, which the array does not have. The triplets are listed in order of their first value, then their second value.
Example 2
nums = [0,0,0,0]return = [[0,0,0]]Any three of the four zeros add up to zero, but they all give the same values [0, 0, 0], so it is listed once.
Example 3
nums = [5,1,-7,3]return = []There are four ways to choose three of the values: 5 + 1 - 7 = -1, 5 + 1 + 3 = 9, 5 - 7 + 3 = 1 and 1 - 7 + 3 = -3. None of them is zero, so the answer is an empty list.
Constraints
3 <= nums.length <= 3000-10^5 <= nums[i] <= 10^5
More Nutanix Phone Screen questions
- Swap Adjacent Linked List NodesPHONE SCREEN · Seen Mar 2025
- Top K Most Frequent ValuesPHONE SCREEN · Seen Mar 2025
- Find the Duplicate in a Read-Only ArrayPHONE SCREEN · Seen Apr 2021
- Kth Largest Element in an ArrayPHONE SCREEN · Seen Feb 2020
- Kth Smallest Element in a BSTPHONE SCREEN · Seen Feb 2020
- Outline of a Binary TreePHONE SCREEN · Seen Jan 2020
- Print All File and Folder PathsPHONE SCREEN · Seen Dec 2019
- Inorder Predecessor in a BSTPHONE SCREEN · Seen Jan 2019
- K Largest and K Smallest ValuesPHONE SCREEN · Seen Jan 2019
- Odd Numbers Up and BackPHONE SCREEN · Seen Jan 2019
Similar questions at other companies
- 3SumAmazon · PHONE SCREEN
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
- Meeting Rooms IIUber · PHONE SCREENUber interview questions
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Sort ColorsVisa · PHONE SCREENVisa interview questions
- Count Triplets Within a Value RangeIBM · PHONE SCREENIBM interview questions
- 3SumMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Meeting Rooms IIWalmart · PHONE SCREENWalmart interview questions
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREENWhatNot interview questions