Two Number Sum

A corgi smiling happily

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

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

Sample Output

1[-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

Solution-1
1function twoNumberSum(array, targetSum) {
2 // array of length n
3 if (array.length === 0) return
4
5 for (let i of array) {
6 // first n iteration
7 for (let j of array) {
8 // secind n iteration
9 if (i === j) continue
10 if (i + j === targetSum) return [i, j]
11 }
12 }
13 return []
14}
15
16// O(n ^ 2) time, since there are two iterations of array in this solution.
17// O(1) space, since all is just temporary space.
18
19// O(n ^ 2) | O(1)
Solution-2
1function twoNumberSum(array, targetSum) {
2 // array of length n
3 const nums = {}
4 for (let num of array) {
5 // first iteration of n
6 let potentialMatch = targetSum - num
7 if (potentialMatch in nums) {
8 return [potentialMatch, num]
9 } else {
10 nums[num] = true
11 }
12 }
13 return []
14}
15
16// O(n) time, wow this solution is just awesome.
17// O(n) space, since we are creating a dict of nums
Solution-3
1function twoNumberSum(array, targetSum) {
2 array.sort((a, b) => a - b)
3 let left = 0
4 let right = array.length - 1
5
6 while (left < right) {
7 // sum of left index and right index
8 let currentSum = array[left] + array[right]
9 if (currentSum === targetSum) {
10 return [array[left], array[right]]
11 } else if (currentSum < targetSum) {
12 left++
13 } else if (currentSum > targetSum) {
14 right--
15 }
16 }
17 return []
18}

🍉

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 🙏

Subscribe to my newsletter

Get email from me about my ideas, full-stack development resources, tricks and tips as well as exclusive previews of upcoming articles.

No spam. Just the highest quality ideas you’ll find on the web.