Bird
0
0
DSA Cprogramming~10 mins

Three Sum Problem All Unique Triplets in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Three Sum Problem All Unique Triplets
Sort the array
For each element i
Set left = i+1, right = end
While left < right
Sum == 0?
Add triplet
Skip duplicates
Repeat until all i processed
Return all unique triplets
Sort the array, then for each element use two pointers to find pairs that sum to zero with it, skipping duplicates.
Execution Sample
DSA C
int threeSum(int* nums, int numsSize) {
  // Sort nums
  // For i in 0 to numsSize-3
  //   left = i+1, right = numsSize-1
  //   while left < right
  //     sum = nums[i]+nums[left]+nums[right]
  //     if sum == 0 add triplet and skip duplicates
  //     else move pointers
}
Find all unique triplets in the array that sum to zero.
Execution Table
StepOperationileftrightSumTriplets FoundPointer ChangesArray State
1Sort array----[]-[-4, -1, -1, 0, 1, 2]
2Set i=00---[]-[-4, -1, -1, 0, 1, 2]
3Set left=1, right=5015-[]-[-4, -1, -1, 0, 1, 2]
4Calculate sum015-4 + (-1) + 2 = -3[]-[-4, -1, -1, 0, 1, 2]
5Sum < 0, move left025-[]left++[-4, -1, -1, 0, 1, 2]
6Calculate sum025-4 + (-1) + 2 = -3[]-[-4, -1, -1, 0, 1, 2]
7Sum < 0, move left035-[]left++[-4, -1, -1, 0, 1, 2]
8Calculate sum035-4 + 0 + 2 = -2[]-[-4, -1, -1, 0, 1, 2]
9Sum < 0, move left045-[]left++[-4, -1, -1, 0, 1, 2]
10Calculate sum045-4 + 1 + 2 = -1[]-[-4, -1, -1, 0, 1, 2]
11Sum < 0, move left055-[]left++[-4, -1, -1, 0, 1, 2]
12left >= right, move i1---[]-[-4, -1, -1, 0, 1, 2]
13Set left=2, right=5125-[]-[-4, -1, -1, 0, 1, 2]
14Calculate sum125-1 + (-1) + 2 = 0[]-[-4, -1, -1, 0, 1, 2]
15Sum == 0, add triplet1250[[-1,-1,2]]-[-4, -1, -1, 0, 1, 2]
16Skip duplicates left135-[[-1,-1,2]]left++[-4, -1, -1, 0, 1, 2]
17Skip duplicates right134-[[-1,-1,2]]right--[-4, -1, -1, 0, 1, 2]
18Calculate sum134-1 + 0 + 1 = 0[[-1,-1,2]]-[-4, -1, -1, 0, 1, 2]
19Sum == 0, add triplet1340[[-1,-1,2],[-1,0,1]]-[-4, -1, -1, 0, 1, 2]
20Skip duplicates left144-[[-1,-1,2],[-1,0,1]]left++[-4, -1, -1, 0, 1, 2]
21left >= right, move i2---[[-1,-1,2],[-1,0,1]]-[-4, -1, -1, 0, 1, 2]
22i=2 is duplicate of i=1, skip i3---[[-1,-1,2],[-1,0,1]]-[-4, -1, -1, 0, 1, 2]
23Set left=4, right=5345-[[-1,-1,2],[-1,0,1]]-[-4, -1, -1, 0, 1, 2]
24Calculate sum3450 + 1 + 2 = 3[[-1,-1,2],[-1,0,1]]-[-4, -1, -1, 0, 1, 2]
25Sum > 0, move right344-[[-1,-1,2],[-1,0,1]]right--[-4, -1, -1, 0, 1, 2]
26left >= right, move i4---[[-1,-1,2],[-1,0,1]]-[-4, -1, -1, 0, 1, 2]
27i=4, no enough elements for triplet, end----[[-1,-1,2],[-1,0,1]]-[-4, -1, -1, 0, 1, 2]
💡 i reached end where no triplets possible, all unique triplets found
Variable Tracker
VariableStartAfter Step 2After Step 15After Step 23Final
i-0134
left--24-
right--55-
Triplets Found[][][[-1,-1,2]][[-1,-1,2],[-1,0,1]][[-1,-1,2],[-1,0,1]]
Array State[-4,-1,-1,0,1,2][-4,-1,-1,0,1,2][-4,-1,-1,0,1,2][-4,-1,-1,0,1,2][-4,-1,-1,0,1,2]
Key Moments - 3 Insights
Why do we skip duplicate values for i and left pointer?
Skipping duplicates avoids repeated triplets. See steps 22 and 16 where duplicates are skipped to keep triplets unique.
Why do we move left pointer when sum < 0 and right pointer when sum > 0?
Because the array is sorted, moving left pointer increases sum, moving right pointer decreases sum. See steps 4-11 and 24-25.
Why stop when left >= right?
Because left and right pointers cross means all pairs for current i are checked. See step 11 and 20.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 15, what triplet is added?
A[-1, -1, 2]
B[-4, 0, 4]
C[0, 1, -1]
D[-1, 0, 1]
💡 Hint
Check the 'Triplets Found' column at step 15 in the execution table.
At which step does the left pointer move after finding the first triplet?
AStep 16
BStep 14
CStep 18
DStep 20
💡 Hint
Look at the 'Pointer Changes' column after step 15 in the execution table.
If the array was not sorted, which part of the execution would fail?
ASkipping duplicates
BMoving pointers based on sum comparison
CAdding triplets
DSetting initial pointers
💡 Hint
Refer to steps 4-11 and 24-25 where pointer moves depend on sum comparison assuming sorted array.
Concept Snapshot
Three Sum Problem:
- Sort array first
- For each element i, use two pointers left=i+1 and right=end
- Calculate sum = nums[i]+nums[left]+nums[right]
- If sum == 0, add triplet and skip duplicates
- If sum < 0, move left pointer right
- If sum > 0, move right pointer left
- Repeat until all i processed
- Return all unique triplets
Full Transcript
The Three Sum Problem finds all unique triplets in an array that sum to zero. First, the array is sorted to allow two-pointer technique. For each element i, two pointers left and right are set to the next element and the end of the array. The sum of nums[i], nums[left], and nums[right] is calculated. If the sum is zero, the triplet is added to the result and pointers skip duplicates. If the sum is less than zero, the left pointer moves right to increase sum. If the sum is greater than zero, the right pointer moves left to decrease sum. This continues until left meets right. Then i moves forward, skipping duplicates. The process repeats until all elements are processed. The final result contains all unique triplets that sum to zero.