DEV Community

Discussion on: Algorithm Practice: Two Sum

Collapse
 
shwetabh1 profile image
Shwetabh Shekhar

Great job.
You can do it in O(n) if you use a dictionary(but space complexity would be compromised).

  1. Create a dictionary of all elements of the array.
  2. Iterate through the array. From the target-value subtract the current array element and look for it in the map.
  3. If found the current value and the found element in the map will be the target sum pair.