Step 1: Sort the array to [-4, -1, -1, 0, 1, 2]
[-4, -1, -1, 0, 1, 2]
Why: Sorting helps find pairs with two pointers and avoid duplicates
Step 2: Set i=0 (value -4), set left=1, right=5
i=0(-4), left=1(-1), right=5(2)
Why: Start with first number and try to find two others summing to 4
Step 3: Sum = -4 + (-1) + 2 = -3 < 0, move left to 2
i=0(-4), left=2(-1), right=5(2)
Why: Sum too small, increase left to get bigger sum
Step 4: Sum = -4 + (-1) + 2 = -3 < 0, move left to 3
i=0(-4), left=3(0), right=5(2)
Why: Sum still too small, move left pointer
Step 5: Sum = -4 + 0 + 2 = -2 < 0, move left to 4
i=0(-4), left=4(1), right=5(2)
Why: Sum still less than zero, move left pointer
Step 6: Sum = -4 + 1 + 2 = -1 < 0, move left to 5
i=0(-4), left=5(2), right=5(2)
Why: Left meets right, no triplet with i=0
Step 7: Increment i to 1 (value -1), left=2, right=5
i=1(-1), left=2(-1), right=5(2)
Why: Try next number as first element
Step 8: Sum = -1 + (-1) + 2 = 0, found triplet [-1, -1, 2], add to result
result = [[-1, -1, 2]]
Why: Sum zero means valid triplet
Step 9: Move left to 3 and right to 4 to avoid duplicates
i=1(-1), left=3(0), right=4(1)
Why: Look for other pairs with i=1
Step 10: Sum = -1 + 0 + 1 = 0, found triplet [-1, 0, 1], add to result
result = [[-1, -1, 2], [-1, 0, 1]]
Why: Another valid triplet found
Step 11: Move left to 4 and right to 3, pointers cross, increment i
i=2(-1), left=3(0), right=4(1)
Why: No more pairs for i=1, move to next i
Step 12: Skip i=2 because nums[2] == nums[1] to avoid duplicates
i=3(0), left=4(1), right=5(2)
Why: Avoid duplicate triplets starting with same number
Step 13: Sum = 0 + 1 + 2 = 3 > 0, move right to 4
i=3(0), left=4(1), right=4(1)
Why: Sum too big, decrease right pointer
Step 14: Left meets right, no more triplets
End
Result: [-1, -1, 2] -> [-1, 0, 1] -> null