logo CodeStepByStep logo

threeSum

Language/Type: C++ vector loops algorithms interview
Related Links:

Write a function named threeSum that accepts a reference to a vector of integers and prints all combinations of three integers in the list that sum to 0. For example, if given the vector {-1, 0, 1, 2, -1, -4}, print the following lines of output:

-1 0 1
-1 -1 2

You may print the lines of output in any order. Do not print duplicate vectors; if a sub-vector with the same three element values can be made in multiple ways, print it only once. (For example, in the vector above, you should not print the combination 0 1 -1 since it is a duplicate of -1 0 1.) If there are no combinations of three elements that sum to 0, print no output.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.