Skip to content
CodeKruPro

Three numbers that add up to zero (3 sum)

Coding problemMediumAsked in: Adobe, Amazon, Flipkart, Google, Meta, Microsoft

Given a list of integers, find every distinct group of three values, taken from three different positions, that adds up to 0.

Return each group once: two groups with the same three values count as the same group, whatever positions they came from. The groups may be returned in any order, and the values inside a group may be in any order. If no group exists, return an empty list.

Examples

Example 1
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Example 2
Input: nums = [0,1,1]
Output: []
Example 3
Input: nums = [0,0,0,0]
Output: [[0,0,0]]
Many position choices, but only one group of values.

Constraints

  • The list holds between 0 and 3,000 integers
  • Each value is between -100,000 and 100,000

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in