problem1

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: May 30, 2020 License: MIT Imports: 0 Imported by: 0

README

< Previous                  Next >

1. Two Sum (Easy)

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

[Array] [Hash Table]

Similar Questions

  1. 3Sum (Medium)
  2. 4Sum (Medium)
  3. Two Sum II - Input array is sorted (Easy)
  4. Two Sum III - Data structure design (Easy)
  5. Subarray Sum Equals K (Medium)
  6. Two Sum IV - Input is a BST (Easy)
  7. Two Sum Less Than K (Easy)

Hints

Hint 1 A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations.
Hint 2 So, if we fix one of the numbers, say
x
, we have to scan the entire array to find the next number
y
which is
value - x
where value is the input parameter. Can we change our array somehow so that this search becomes faster?
Hint 3 The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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