Cascadiq

3Sum

algorithm · Level 2 · 25 min

Description

Given an integer array nums, return all triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, j != k, and nums[i] + nums[j] + nums[k] == 0. Solution set must not contain duplicate triplets.

Requirements

  • Implement a function that takes nums (array of integers).
  • Return an array of all unique triplets that sum to 0.

Constraints

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

Deliverables

Return array of triplets (each triplet is an array of three numbers).

Tags

ArrayTwo PointersSorting