Leetcode Problem - 3Sum
๐ง Problem
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. Notice that the solution set must not contain duplicate triplets.
๐ท๏ธ Tags
Array,Two Pointers,Sorting
๐ Difficulty
Medium
โ
Success Rate: 31%
๐ฅ Submissions: 6,090,299
๐ Accepted: 1,886,780
โค๏ธ Reactions
๐ Likes: 17218
๐ Dislikes: 1653
๐ก Hints
So, we essentially need to find three numbers x, y, and z such that they add up to the given value. If we fix one of the numbers say x, we are left with the two-sum problem at hand! For the two-sum problem, if we fix one of the numbers, say x, we have to scan the entire array to find the next numbery which is value - x where value is the input parameter. Can we change our array somehow so that this search becomes faster? The second train of thought for two-sum is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?
๐ Similar Questions
- Two Sum
- 3Sum Closest
- 4Sum
- 3Sum Smaller