Three Sum

Coding · Medium · Free problem
Given an array of $n$ integers (not necessarily sorted), find all unique triplets $(a, b, c)$ such that $a + b + c = 0$. The triplets must use elements at distinct indices, and the output should contain no duplicate triplets. **Constraints:** - $3 \le n \le 3000$ - $-10^5 \le \text{nums}[i] \le 10^5$ **Example 1:** - Input: `nums = [-1, 0, 1, 2, -1, -4]` - Output: `[[-1, -1, 2], [-1, 0, 1]]` - Explanation: `nums[0] + nums[1] + nums[2] = -1 + 0 + 1 = 0` and `nums[0] + nums[3] + nums[4] = -1 + 2 + (-1) = 0` (after deduplication). **Example 2:** - Input: `nums = [0, 0, 0]` - Output: `[[0, 0, 0]]` **Example 3:** - Input: `nums = [1, 2, -3, 4]` - Output: `[[-3, 1, 2]]`

Open the full interactive solver, hints, and worked solution →