two_number_sum

package
v0.0.0-...-86b9fec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2022 License: MIT Imports: 1 Imported by: 0

README

Two Number Sum

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

Sample Output

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TwoNumberSum

func TwoNumberSum(array []int, target int) []int

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL