3Sum
Medium Two Pointers
nums is a list of whole numbers.
A triplet is three numbers of nums, taken from three different positions, that add up to 0. Two triplets that hold the same three numbers, in any order, count as one.
Return every different triplet, each as a list of its three numbers. The triplets may come in any order, and so may the numbers inside each one. Return an empty list when there is none.
Example 1
Input: nums = [8,-5,3,-3,2,-10,5,0]
One right answer: [[-10,2,8],[-5,-3,8],[-5,0,5],[-5,2,3],[-3,0,3]]
Five triplets add up to 0: -10 + 2 + 8, -5 + -3 + 8, -5 + 0 + 5, -5 + 2 + 3 and -3 + 0 + 3.
Example 2
Input: nums = [4,-2,-2,9,-7]
One right answer: [[-7,-2,9],[-2,-2,4]]
Example 3
Input: nums = [5,6,-1,-3]
One right answer: []
Constraints
3 ≤ nums.length ≤ 4000-10⁶ ≤ nums[i] ≤ 10⁶numsholds at most10⁴different triplets
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundWatch the explanation
More Two Pointers
- Trapping Rain WaterAI round · Hard
- Container With Most WaterAI round · Medium
- Longest Palindromic SubstringAI round · Medium
- Find the Duplicate NumberAI round · Medium
- Sort ColorsAI round · Medium