 All Problems
3Sum
medium
arrays
two pointers
amazon
facebook
microsoft
adobe

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i, j, k are distinct indices and their sum is 0. The solution set must not contain duplicate triplets.

Example 1:

Input: -1 0 1 2 -1 -4
Output:
-1 -1 2
-1 0 1

Example 2:

Input: 0 1 1
Output:
(empty)

Constraints:

  • 3 ≤ nums.length ≤ 3000
  • -10⁵ ≤ nums[i] ≤ 10⁵

Input format: Space-separated integers.

Output format: Each triplet on a new line, space-separated, sorted ascending. If no triplets, print nothing.

Run to check your code against the sample cases, or submit to run every case