Step 1: Sort the array to get [-4, -1, -1, 0, 1, 2]
[-4, -1, -1, 0, 1, 2]
Why: Sorting helps to use two pointers and avoid duplicates easily
Step 2: Set i=0 (value -4), set left=1, right=5
i=-4, left=-1, right=2
Why: Start with first element and two pointers to find pairs that sum with i to zero
Step 3: Calculate sum = -4 + (-1) + 2 = -3 < 0, move left to 2
i=-4, left=-1 (index 2), right=2
Why: Sum less than zero means we need a bigger number, move left pointer right
Step 4: Calculate sum = -4 + (-1) + 2 = -3 < 0, move left to 3
i=-4, left=0, right=2
Why: Still less than zero, move left pointer right
Step 5: Calculate sum = -4 + 0 + 2 = -2 < 0, move left to 4
i=-4, left=1, right=2
Why: Sum less than zero, move left pointer right
Step 6: Calculate sum = -4 + 1 + 2 = -1 < 0, move left to 5
i=-4, left=2, right=2
Why: Sum less than zero, move left pointer right but now left == right, stop
Step 7: Increment i to 1 (value -1), set left=2, right=5
i=-1, left=-1, right=2
Why: Move to next element to find new triplets
Step 8: Calculate sum = -1 + (-1) + 2 = 0, found triplet [-1, -1, 2], move left and right skipping duplicates
triplets = [[-1, -1, 2]]
Why: Sum zero means valid triplet, move pointers to find others
Step 9: left moves to 3 (0), right moves to 4 (1)
i=-1, left=0, right=1
Why: Continue searching for other pairs with i
Step 10: Calculate sum = -1 + 0 + 1 = 0, found triplet [-1, 0, 1], move left and right skipping duplicates
triplets = [[-1, -1, 2], [-1, 0, 1]]
Why: Found another valid triplet
Step 11: left moves to 4, right moves to 3, pointers cross, increment i to 2
i=-1 (index 2), left=3, right=4
Why: No more pairs for this i, move to next i
Step 12: Skip i=2 because nums[2] == nums[1] to avoid duplicates
i=3 (value 0)
Why: Avoid duplicate triplets by skipping same values for i
Step 13: Set left=4 (1), right=5 (2), calculate sum = 0 + 1 + 2 = 3 > 0, move right left
i=0, left=1, right=2
Why: Sum too big, move right pointer left to reduce sum
Step 14: right moves to 4, left == right, stop
No new triplets
Why: Pointers crossed, no more pairs for i=3
Result: [-1, -1, 2] -> [-1, 0, 1]