You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 3Sum and 4Sum/README.md
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,6 +70,53 @@ extension BidirectionalCollection where Element: Equatable {
70
70
}
71
71
```
72
72
73
+
### Assembling the Subsets
74
+
75
+
You'll keep track of 3 indices to represent the 3 numbers. The sum at any given moment is `array[l] + array[m] + array[r]`:
76
+
77
+
```
78
+
m -> <- r
79
+
[-4, -1, -1, 0, 1, 2]
80
+
l
81
+
```
82
+
83
+
The premise is quite straightforward (given that you're familiar with 2Sum). You'll iterate `l` through the array. For every iteration, you also apply the 2Sum algorithm to elements after `l`. You'll check the sum every time you moving the indices to check if you found match. Here's the algorithm:
0 commit comments