Skip to content

Commit 38e284f

Browse files
committed
Update twoSum’s README with a simpler dictionary.
1 parent 5eb53e0 commit 38e284f

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

Two-Sum Problem/README.markdown

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,82 @@
11
# Two-Sum Problem
22

3-
You're given an array of numbers. Write an algorithm that checks if there are any two elements in the array that add up to a given number.
3+
Given an array of integers and an integer target, return the indices of two numbers that add up to the target.
44

55
There are a variety of solutions to this problem (some better than others). The following solutions both run in **O(n)** time.
66

77
# Solution 1
88

9-
This solution uses a dictionary to store differences between each element in the array and the sum that we're looking for. The dictionary also stores the indices of each element.
9+
This solution looks at one number at a time, storing each number in the dictionary. It uses the number as the key and the number's index in the array as the value.
1010

11-
With this approach, each key in the dictionary corresponds to a new target value. If one of the successive numbers from the array is equal to one of the dictionary's keys, then we know there exist two numbers that sum.
11+
For each number n, we know the complementing number to sum up to the target is `target - n`. By looking up the complement in the dictionary, we'd know whether we've seen the complement before and what its index is.
1212

1313
```swift
14-
func twoSumProblem(_ numbers: [Int], target: Int) -> (Int, Int)? {
15-
var dict: [Int: Int] = [:]
16-
17-
for (index, number) in numbers.enumerated() {
18-
if let otherIndex = dict[number] {
19-
return (index, otherIndex)
20-
} else {
21-
dict[target - number] = index
14+
func twoSum(_ nums: [Int], target: Int) -> (Int, Int)? {
15+
var dict = [Int: Int]()
16+
17+
// For every number n,
18+
for (currentIndex, n) in nums.enumerated() {
19+
// Find the complement to n that would sum up to the target.
20+
let complement = target - n
21+
22+
// Check if the complement is in the dictionary.
23+
if let complementIndex = dict[complement] {
24+
return (complementIndex, currentIndex)
25+
}
26+
27+
// Store n and its index into the dictionary.
28+
dict[n] = currentIndex
2229
}
23-
}
24-
25-
return nil // if empty array or no entries sum to target k
30+
31+
return nil
2632
}
2733
```
2834

29-
The `twoSumProble` function takes two parameters: the `numbers` array and the target sum you're looking for. It returns the 2 indices with elements that sums up to the target, or `nil` if it can't be found.
35+
The `twoSum` function takes two parameters: the `numbers` array and the target sum. It returns the two indicies of the pair of elements that sums up to the target, or `nil` if they can't be found.
3036

31-
Let's take a look at an example and run through the algorithm to see how it works. Given is the array:
37+
Let's run through the algorithm to see how it works. Given the array:
3238

3339
```swift
34-
[ 7, 2, 23, 8, -1, 0, 11, 6 ]
40+
[3, 2, 9, 8]
3541
```
3642

37-
Let's find out if there exist two entries whose sum is equal to 10.
43+
Let's find out if there exist two entries whose sum is 10.
3844

3945
Initially, our dictionary is empty. We begin looping through each element:
4046

41-
- **i = 0**: Is `7` in the dictionary? No. We add the difference between the `target` and the current number to the dictionary. The difference is `10 - 7 = 3`, so the dictionary key is `3`. The value for that key is the current index, `0`. The dictionary now looks like this:
47+
- **currentIndex = 0** | n = nums[0] = 3 | complement = 10 - 3 = 7
48+
49+
Is the complement `7` in the dictionary? No, so we add `3` and its index `0` to the dictionary.
4250

4351
```swift
44-
[ 3: 0 ]
52+
[3: 0]
4553
```
4654

47-
- **i = 1:** Is `2` in the dictionary? No. Let's do as above and add the difference (`10 - 2 = 8`) and the array index (`1`). The dictionary is:
55+
- **currentIndex = 1** | n = 2 | complement = 10 - 2 = 8
56+
57+
Is the complement `8` in the dictionary? No, so we add `2` and its index `1` to the dictionary.
4858

4959
```swift
50-
[ 3: 0, 8: 1 ]
60+
[3: 0, 2: 1]
5161
```
5262

53-
- **i = 2:** Is `23` in the dictionary? No. Again, we add it to the dictionary. The difference is `10 - 23 = -13` and the index is `2`:
63+
- **currentIndex = 2** | n = 9 | complement = 10 - 9 = 1
64+
65+
Is the complement `1` in the dictionary? No, so we add `9` and its index `2` to the dictionary.:
5466

5567
```swift
56-
[ 3: 0, 8: 1, -13: 2 ]
68+
[3: 0, 2: 1, 9: 2]
5769
```
5870

59-
- **i = 3:** Is `8` in the dictionary? Yes! That means that we have found a pair of entries that sum to our target. Namely the current number `8` and `array[dict[8]]` because `dict[8] = 1`, `array[1] = 2`, and `8 + 2 = 10`. Therefore, we return the corresponding indices of these numbers. For `8` that is the current loop index, `3`. For `2` that is `dict[8]` or `1`. The tuple we return is `(1, 3)`.
71+
- **currentIndex = 3** | n = 8 | complement = 10 - 8 = 2
72+
73+
Is the complement `2` in the dictionary? Yes! That means that we have found a pair of entries that sum to the target!
74+
75+
Therefore, the `complementIndex = dict[2] = 1` and the `currentIndex = 3`. The tuple we return is `(1, 3)`.
6076

61-
The given array actually has multiple solutions: `(1, 3)` and `(4, 6)`. However, only the first solution is returned.
77+
If the given array has multiple solutions, only the first solution is returned.
6278

63-
The running time of this algorithm is **O(n)** because it potentially may need to look at all array elements. It also requires **O(n)** additional storage space for the dictionary.
79+
The running time of this algorithm is **O(n)** because it may look at every element in the array. It also requires **O(n)** additional storage space for the dictionary.
6480

6581
# Solution 2
6682

Two-Sum Problem/Solution 1/2Sum.playground/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ func twoSum(_ nums: [Int], target: Int) -> (Int, Int)? {
2020
return nil
2121
}
2222

23-
twoSum([3, 2, 8, 4], target: 6) // expected output: indices 1 and 3
23+
twoSum([3, 2, 9, 8], target: 10) // expected output: indices 1 and 3

0 commit comments

Comments
 (0)