Skip to content
BASA DSA

3Sum

MediumTwo Pointers

Return all distinct value triples whose sum is zero, using three distinct indices. Triple and result ordering are unrestricted.

Example 1

Input
nums = [-2,0,2,2,-2]
Output
[[-2,0,2]]

Explanation

Only -2,0,2 sums to zero; repeated occurrences must not duplicate the answer.

Example 2

Input
nums = [0,0,0,0]
Output
[[0,0,0]]

Explanation

Three distinct zero indices form one value triple even with four zeroes available.

Example 3

Input
nums = [1,2,3]
Output
[]

Explanation

All values are positive, so no triple sums to zero.

Constraints

  • 0 <= len(nums) <= 3000
  • Values are -100000..100000

Function contractthreeSumArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…