Step 1: Sort the array
[-4, -1, -1, 0, 1, 2]
Why: Sorting helps to avoid duplicates and use two-pointer technique
Step 2: Set i=0 (value -4), set left=1, right=5
i=-4 ↑, left=-1 ->, right=2 ←
Why: Start checking triplets with first element -4
Step 3: Sum = -4 + (-1) + 2 = -3 < 0, move left pointer right
i=-4 ↑, left=-1 ->, right=2 ← (left moves to index 2)
Why: Sum too small, increase sum by moving left pointer
Step 4: Sum = -4 + (-1) + 2 = -3 < 0, move left pointer right
i=-4 ↑, left=-1 ->, right=2 ← (left moves to index 3)
Why: Sum still too small, move left pointer again
Step 5: Sum = -4 + 0 + 2 = -2 < 0, move left pointer right
i=-4 ↑, left=0 ->, right=2 ← (left moves to index 4)
Why: Sum still less than zero, move left pointer
Step 6: Sum = -4 + 1 + 2 = -1 < 0, move left pointer right
i=-4 ↑, left=1 ->, right=2 ← (left meets right, stop)
Why: No triplet found with i=0, move to next i
Step 7: Set i=1 (value -1), left=2, right=5
i=-1 ↑, left=-1 ->, right=2 ←
Why: Check triplets starting with -1
Step 8: Sum = -1 + (-1) + 2 = 0, record triplet [-1, -1, 2], move left and right skipping duplicates
Triplets: [-1, -1, 2]
left moves to 3, right moves to 4
Why: Found a valid triplet, move pointers to find others
Step 9: Sum = -1 + 0 + 1 = 0, record triplet [-1, 0, 1], move left and right skipping duplicates
Triplets: [-1, -1, 2], [-1, 0, 1]
left moves to 4, right moves to 3 (left >= right, stop)
Why: Found another valid triplet, no more with i=1
Step 10: Set i=2 (value -1), skip because same as previous i
Skip i=2 to avoid duplicate triplets
Why: Avoid duplicate triplets by skipping same values for i
Step 11: Set i=3 (value 0), left=4, right=5
i=0 ↑, left=1 ->, right=2 ←
Why: Check triplets starting with 0
Step 12: Sum = 0 + 1 + 2 = 3 > 0, move right pointer left
i=0 ↑, left=1 ->, right=1 ← (left >= right, stop)
Why: Sum too big, decrease sum by moving right pointer
Result: Triplets found:
[-1, -1, 2]
[-1, 0, 1]