Python: Combine three numbers to get the target number
28. Unique Combinations of 3 Numbers Adding Up to Target
Write a Python program to find all the unique combinations of 3 numbers from a given list of numbers, adding up to a target number.
Sample Solution:
Python Code:
Sample Output:
Original list of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9] Target value: 12 Combine three numbers whose sum equal to a target number: [(1, 3, 8), (1, 4, 7), (1, 2, 9), (1, 5, 6), (3, 4, 5), (2, 3, 7), (2, 4, 6)] Original list of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9] Target value: 17 Combine three numbers whose sum equal to a target number: [(4, 5, 8), (2, 6, 9), (3, 5, 9), (2, 7, 8), (4, 6, 7), (3, 6, 8), (1, 7, 9)]
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to find all unique triplets in a list that sum to a given target using a set to avoid duplicates.
- Write a Python program to implement a function that returns all unique combinations of three numbers that add up to the target using backtracking.
- Write a Python program to sort a list and then use two pointers to identify triplets summing to a target, ensuring uniqueness with a set.
- Write a Python program to generate combinations of three numbers from a list and filter those whose sum equals the target value.
Go to:
Previous: Find and group all the anagrams in the given list.
Next: Third largest number from a list of numbers using set.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.