Two-sum existence

Given an unsorted array and a target, which approach decides in O(n)O(n) time whether two elements sum to the target?

Show hints (2)+
  1. You only need existence, not the indices.
  2. Trade space for time with a set of seen values.

Answer

Reveal answer →

Single pass with a hash set, checking $\text{target}-x$ — $O(n)$

Want the full step-by-step worked solution? It's part of Premium — along with a worked solution for every question in the bank.

Asked at: Jane Street, Two Sigma

Related questions