Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Two-Sum Problem/README.markdown
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Two-Sum Problem

You're given an array `a` with numbers. Write an algorithm that checks if there are any two entries in the array that add up to a given number `k`. In other words, is there any `a[i] + a[j] == k`?
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.

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

# Solution 1

This solution uses a dictionary to store differences between each element in the array and the sum `k` that we're looking for. The dictionary also stores the indices of each element.
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.

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 to `k`.
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.

```swift
func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? {
var dict = [Int: Int]()

for i in 0 ..< a.count {
if let newK = dict[a[i]] {
return (newK, i)
func twoSumProblem(_ numbers: [Int], target: Int) -> (Int, Int)? {
var dict: [Int: Int] = [:]
for (index, number) in numbers.enumerated() {
if let otherIndex = dict[number] {
return (index, otherIndex)
} else {
dict[k - a[i]] = i
dict[target - number] = index
}
}

Expand Down
27 changes: 11 additions & 16 deletions Two-Sum Problem/Solution 1/2Sum.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@
print("Hello, Swift 4!")
#endif

func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? {
var map = [Int: Int]()

for i in 0 ..< a.count {
if let newK = map[a[i]] {
return (newK, i)
func twoSum(_ numbers: [Int], target: Int) -> (Int, Int)? {
var dict: [Int: Int] = [:]
for (index, number) in numbers.enumerated() {
if let otherIndex = dict[number] {
return (index, otherIndex)
} else {
map[k - a[i]] = i
dict[target - number] = index
}
}

return nil
}

let a = [7, 100, 2, 21, 12, 10, 22, 14, 3, 4, 8, 4, 9]
if let (i, j) = twoSumProblem(a, k: 33) {
i // 3
a[i] // 21
j // 4
a[j] // 12
a[i] + a[j] // 33
}
let numbers = [3, 2, 4]
let target = 6

twoSumProblem(a, k: 37) // nil
twoSum(numbers, target: target) // expected output: indices 2 and 1