← Back to challenges

Triplet Sum

PythonHardloopsarraysnumberslogic

Instructions

Create a function that takes an unsorted list of numbers and returns the number of unique triplets whose sum is equal to the variable n.

Examples

triplet_sum([1, 0, 2, 6, 3, 9, 3], n = 11) ➞ 2
# Since we found two unique triplets that equate to 11: 0+2+9 and 2+6+3

triplet_sum([1, 2, 6, 3, 4, 5, 9, 10, 11], n = 20) ➞ 5

triplet_sum([5, 2, 8], n = 2) ➞ 0

Notes

You should also expect lists with less than three elements.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.