# Two Number Sum

Discover the optimal solutions to the classic problem of finding two numbers that add up to a specific target sum.

Published At

6/3/2021

Reading Time

~ 4 min read

Write a function that takes in a non-empty array of distinct integers and an integer representing a target sum. If any two numbers in the input array sum up to the target sum, the function should return them in an array, in any order. If no two numbers sum up to the target sum, the function should return an empty array.

Note that the target sum has to be obtained by summing two different integers in the array; you can't add a single integer to itself in order to obtain the target sum.

You can assume that there will be at most one pair of numbers summing up to the target sum.

### Sample Input

```
array = [3, 5, -4, 8, 11, 1, -1, 6]
targetSum = 10
```

```
array = [3, 5, -4, 8, 11, 1, -1, 6]
targetSum = 10
```

### Sample Output

`[-1, 11] // the numbers could be in reverse order`

`[-1, 11] // the numbers could be in reverse order`

### Hints

**Hint 1**

Try using two for loops to sum all possible pairs of numbers in the input array. What are the time and space implications of this approach?

**Hint 2**

Realize that for every number X in the input array, you are essentially trying to find a corresponding number Y such that X + Y = targetSum. With two variables in this equation known to you, it shouldn't be hard to solve for Y.

**Hint 3**

Try storing every number in a hash table, solving the equation mentioned in Hint #2 for every number, and checking if the Y that you find is stored in the hash table. What are the time and space implications of this approach?

**Optimal Space & Time Complexity**

`O(n) time`

| `O(n) space`

- where `n`

is the length of the input array

```
function twoNumberSum(array, targetSum) {
// array of length n
if (array.length === 0) return
for (let i of array) {
// first n iteration
for (let j of array) {
// secind n iteration
if (i === j) continue
if (i + j === targetSum) return [i, j]
}
}
return []
}
// O(n ^ 2) time, since there are two iterations of array in this solution.
// O(1) space, since all is just temporary space.
// O(n ^ 2) | O(1)
```

```
function twoNumberSum(array, targetSum) {
// array of length n
if (array.length === 0) return
for (let i of array) {
// first n iteration
for (let j of array) {
// secind n iteration
if (i === j) continue
if (i + j === targetSum) return [i, j]
}
}
return []
}
// O(n ^ 2) time, since there are two iterations of array in this solution.
// O(1) space, since all is just temporary space.
// O(n ^ 2) | O(1)
```

```
function twoNumberSum(array, targetSum) {
// array of length n
const nums = {}
for (let num of array) {
// first iteration of n
let potentialMatch = targetSum - num
if (potentialMatch in nums) {
return [potentialMatch, num]
} else {
nums[num] = true
}
}
return []
}
// O(n) time, wow this solution is just awesome.
// O(n) space, since we are creating a dict of nums
```

```
function twoNumberSum(array, targetSum) {
// array of length n
const nums = {}
for (let num of array) {
// first iteration of n
let potentialMatch = targetSum - num
if (potentialMatch in nums) {
return [potentialMatch, num]
} else {
nums[num] = true
}
}
return []
}
// O(n) time, wow this solution is just awesome.
// O(n) space, since we are creating a dict of nums
```

```
function twoNumberSum(array, targetSum) {
array.sort((a, b) => a - b)
let left = 0
let right = array.length - 1
while (left < right) {
// sum of left index and right index
let currentSum = array[left] + array[right]
if (currentSum === targetSum) {
return [array[left], array[right]]
} else if (currentSum < targetSum) {
left++
} else if (currentSum > targetSum) {
right--
}
}
return []
}
```

```
function twoNumberSum(array, targetSum) {
array.sort((a, b) => a - b)
let left = 0
let right = array.length - 1
while (left < right) {
// sum of left index and right index
let currentSum = array[left] + array[right]
if (currentSum === targetSum) {
return [array[left], array[right]]
} else if (currentSum < targetSum) {
left++
} else if (currentSum > targetSum) {
right--
}
}
return []
}
```

🍉

Do you have any questions, or simply wish to contact me privately? Don't hesitate to shoot me a DM on Twitter.

Have a wonderful day.

Abhishek 🙏