Skip to main content

Command Palette

Search for a command to run...

Leetcode Problem - 3Sum

Published
โ€ข1 min readโ€ขView as Markdown

๐Ÿง  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

More from this blog

ReStart LeetCode

540 posts