2SUM

March 10, 2020

We’ve done this in a previous exercise, but it’s a common problem both as an interview question and in programming classes, and I’ve seen in it several times in the last week, so now is a good time to do it again:

Given a list of integers and a target integer, find all the pairs of integers in the list that sum to the target integer, or report that there are no such pairs.

Your task is to write a program to find pairs of integers that sum to a target; you should write three programs, with time complexities of O(n²), O(n log n), and O(n). When you are finished, you are welcome to read a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2