Mark As Completed Discussion

Good afternoon! Here's our prompt for today.

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. The solution must not contain any duplicate triplets, and the order of numbers in the triplet must be in ascending order.

Question

For example, consider the following array,

SNIPPET
1nums = [-2, 1, -3, 5, -3, 5]

If we look closely, there are a lot of duplicate elements here. The unique triplet that gives the sum as 0 is [-3, -2, 5]. This triplet can be formed from numbers at indices 0, 2, and 3. It can also be formed from numbers at indices 0, 4, and 5. Since we are avoiding duplicates, we consider only one unique triplet to be the correct solution, regardless of the indices.

Constraints

  • 3 <= nums.length <= 3000
  • 105 <= nums[i] <= 105

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's how we would solve this problem...

How do I use this guide?

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.