FastPrep3Sum

3Sum

AT&T logoAT&T● MediumNEW GRADOA
Learn

Problem statement

Given an integer array nums, return every unique triplet [a, b, c] whose values sum to 0.

Each returned triplet must be sorted in ascending order. Return the complete list of triplets in lexicographic order, without duplicates.

Function

threeSum(nums: int[]) → int[][]

Examples

Example 1

nums = [-1,0,1,2,-1,-4]return = [[-1,-1,2],[-1,0,1]]

The two unique value triplets that sum to 0 are [-1,-1,2] and [-1,0,1].

Example 2

nums = [0,0,0,0]return = [[0,0,0]]

All valid index choices produce the same value triplet, so [0,0,0] appears only once.

Constraints

  • 3 <= nums.length <= 3000.
  • -100000 <= nums[i] <= 100000.

More AT&T problems

See AT&T hiring insights
public int[][] threeSum(int[] nums) {
  // write your code here
}
nums[-1,0,1,2,-1,-4]
expected[[-1,-1,2],[-1,0,1]]
Checking account…