FastPrepThree Sum: Unique Zero-Sum Triplets

Three Sum: Unique Zero-Sum Triplets

Google logoGoogle● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

Given an integer array nums, return every distinct value triplet whose sum is 0.

  • Each triplet must be nondecreasing.
  • The returned list must contain no duplicate value triplets.
  • Return the triplets in lexicographic order.

Use 64-bit arithmetic when computing sums.

Function

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

Examples

Example 1

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

The two distinct value triplets with sum 0 are [-1,-1,2] and [-1,0,1].

Example 2

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

Many index triples exist, but they all produce the same value triplet, so [0,0,0] appears once.

Example 3

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

No three values sum to 0.

Constraints

  • 3 <= nums.length <= 3000.
  • -10^9 <= nums[i] <= 10^9.

More Google problems

See Google 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…