DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Array

3Sum

**Problem Statement:** 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. **Hint / Expected Approach:** Sort + two-pointer, skip duplicates **Edge Cases to Consider:** - (1) No valid triplets - (2) All zeros - (3) Array shorter than 3

Examples

Example 1
Input: [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Example 2
Input: [0,1,1]
Output: []
Example 3
Input: [0,0,0]
Output: [[0,0,0]]

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪No valid triplets
  • ▪All zeros
  • ▪Array shorter than 3
Frequently Asked At
GoogleAmazonMicrosoftMetaAdobeUber