← Back to DSA Animator
3Sum LC #15 Medium Two Pointers
Approach
Sort + Two Pointer
Sort the array, fix pivot i, run two pointers L & R on the rest. Skip duplicates. O(n²) time.
sum = ?
Triplets Found
i (pivot)
L
R
sum
Press Play to start
Unique triplets summing to zero
Algorithm
1
Sort the array
2
For each i: skip duplicates (nums[i]==nums[i-1])
3
Two pointer L=i+1, R=n-1 on remaining
4
sum==0 → save+skip dups; sum<0 → L++; sum>0 → R--
Time
O(n²)
Space
O(1)
Why It Works

After sorting, fix one element and reduce to TwoSum = −pivot on the remainder.

Two pointer works because the array is sorted — move L right to increase sum, R left to decrease sum.

Skip duplicates by checking adjacent values to avoid repeated triplets.