The Triple Seeker

HardAcc. 68.5%
+50 XP 25

Combinatorial Balance

Given an array, find all unique triplets $[a, b, c]$ such that $a + b + c = 0$.

The Assignment

Your function receives nums.

  1. First, sort the array.
  2. Iterate through with index i. Skip duplicates.
  3. For each i, use Two Pointers (left = i+1, right = end) to find sums that equal -nums[i].
  4. Print each triplet on a new line (format: a b c).

01EXAMPLE 1

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

Explanation: Unique triplets.

Constraints

  • Avoid duplicate triplets.
  • O(n^2) time complexity.
ArraysTwo Pointers
JavaScript
Loading...
1 Hidden

Input Arguments

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

Expected Output

-1 -1 2
-1 0 1

Click RUN to test your solution