Skip to content
Onsite

Triplet with Sum 0

MediumP1Coding

Given an integer array nums, return all unique triplets [a, b, c] of values (taken from three different indices) such that a + b + c = 0.

The result must not contain duplicate triplets. Return the triplets in any order, and the values inside a triplet in any order.

Example 1

Input
nums = [-1,0,1,2,-1,-4]
Output
[[-1,-1,2],[-1,0,1]]

Example 2

Input
nums = [0,1,1]
Output
[]

Example 3

Input
nums = [0,0,0,0]
Output
[[0,0,0]]

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 8.