Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

25. 3Sum

MediumDay 7 of 60Two Pointers

Given an integer array nums, return every unique triplet [nums[i], nums[j], nums[k]] with i, j, k all different whose sum is 0. The result must not contain the same triplet twice. The triplets and the numbers inside them may be in any order.

Example 1
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
-1 + -1 + 2 = 0 and -1 + 0 + 1 = 0. Using the other -1 gives the same triplets, so they are listed once.
Example 2
Input: nums = [0,1,1]
Output: []
No three numbers add up to 0.
Constraints
  • 3 <= nums.length <= 3000
  • -10^5 <= nums[i] <= 10^5
Hints
Frequently asked at: Meta, Amazon, Microsoft, Google, Adobe, Bloomberg
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min