Applied AI Engineer (LLM) coding learning path Level 2 This level · 27 of 31

3Sum

Medium Two pointers

Problem

Given an integer array, return all unique triplets [a, b, c] such that the three values are at different indices and a + b + c = 0. The triplets may be returned in any order, but each unique combination of values should appear once.

Original: LeetCode 15: 3Sum.

Examples

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]`
Output`[[0, 0, 0]]`

Constraints

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

Hints

Solution

Tried it yourself? The approach, complexity, and code stay hidden until you choose.

Sources (3)
  1. LeetCode 15: 3Sumleetcode.com
  2. Java SE 17 API: Arrays.sortdocs.oracle.com
  3. LeetCode Discuss: Microsoft SDE-2 recent questions 2025 (candidate reports, secondary)leetcode.com
Previous
Next