@@ -32,3 +32,61 @@ Input: nums = [0,0,0]
3232Output: [[0,0,0]]
3333Explanation: The only possible triplet sums up to 0.
3434```
35+
36+ ## Solution
37+
38+ We can leverage the two-pointer technique to solve this problem by first sorting the array. We can then iterate through
39+ each element in the array. The problem then reduces to finding two numbers in the rest of the array that sum to the
40+ negative of the current element, which follows the same logic as the Two Sum (Sorted Array) problem.
41+
42+ ![ Solution 1] ( ./images/solutions/three_sum_solution_1.png )
43+
44+ Since our first triplet sums to 0, we can add it to our result set.
45+
46+ ![ Solution 2] ( ./images/solutions/three_sum_solution_2.png )
47+ ![ Solution 3] ( ./images/solutions/three_sum_solution_3.png )
48+ ![ Solution 4] ( ./images/solutions/three_sum_solution_4.png )
49+ ![ Solution 5] ( ./images/solutions/three_sum_solution_5.png )
50+ ![ Solution 6] ( ./images/solutions/three_sum_solution_6.png )
51+
52+ ### Avoiding Duplicates
53+
54+ As soon as we find a triplet that sums to 0, we can add it to our result set. We then have to move our left and right
55+ pointers to look for the next triplet while avoiding duplicate triplets. We can do this by moving the left and right
56+ pointers until they point to different numbers than the ones they were pointing to before.
57+ Here we move the left pointer once until it reaches the last -1 in the array. Then, we can move both the left and right
58+ pointers so that they both point to new numbers.
59+
60+ ![ Solution 7] ( ./images/solutions/three_sum_solution_7.png )
61+ ![ Solution 8] ( ./images/solutions/three_sum_solution_8.png )
62+
63+ Here we can do another iteration of the Two Sum problem using the new positions of the left and right pointers.
64+
65+ ![ Solution 9] ( ./images/solutions/three_sum_solution_9.png )
66+ ![ Solution 10] ( ./images/solutions/three_sum_solution_10.png )
67+ ![ Solution 11] ( ./images/solutions/three_sum_solution_11.png )
68+
69+ At this point our left and right pointers have crossed, so we can move our iterator to the next number in the array.
70+
71+ ### Avoiding Duplicates II
72+
73+ In this case, since the next number in the array is the same as the previous number, we can skip it. We can do this by
74+ moving our iterator until it points to a new number.
75+
76+ ![ Solution 12] ( ./images/solutions/three_sum_solution_12.png )
77+ ![ Solution 13] ( ./images/solutions/three_sum_solution_13.png )
78+ ![ Solution 14] ( ./images/solutions/three_sum_solution_14.png )
79+
80+ And we're ready to start the Two Sum algorithm again, so we reset our left and right pointers, and start the algorithm.
81+
82+ ![ Solution 15] ( ./images/solutions/three_sum_solution_15.png )
83+ ![ Solution 16] ( ./images/solutions/three_sum_solution_16.png )
84+ ![ Solution 17] ( ./images/solutions/three_sum_solution_17.png )
85+
86+ ### Termination
87+
88+ Our algorithm terminates when i reaches the 3rd to last element in the array (i.e., i < n - 2). This is because we need
89+ at least 2 more elements after i for left and right to form a triplet.
90+
91+ ![ Solution 18] ( ./images/solutions/three_sum_solution_18.png )
92+ ![ Solution 19] ( ./images/solutions/three_sum_solution_19.png )
0 commit comments