FastPrep3Sum

3Sum

Microsoft logoMicrosoftMediumFULLTIMEPHONE SCREEN
Learn

Problem statement

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

Output Rules

  • Sort each triplet in ascending order.
  • Sort the list of triplets lexicographically.
  • Do not return duplicate triplets.

Function

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

Examples

Example 1

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

These are the only two distinct value triplets with sum zero.

Example 2

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

The same value triplet is returned only once.

Constraints

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

More Microsoft problems

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