FastPrep3Sum

3Sum

Apple logoApple● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Find every distinct triple of values in nums whose sum is 0. A triple must use three different array positions.

Return each value triple in nondecreasing order, remove duplicate triples, and return the rows in lexicographic order.

Function

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

Examples

Example 1

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

Repeated input values can create the same value triple, which is returned only once.

Example 2

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

The only three-position selection does not sum to zero.

Example 3

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

Three separate zero positions form one triple.

Constraints

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

More Apple problems

See Apple hiring insights
public int[][] threeSum(int[] nums) {
    // Write your solution here.
}
nums[-1,0,1,2,-1,-4]
expected[[-1,-1,2],[-1,0,1]]
Checking account…