#K34337. Find All Unique Triplets that Sum up to Target
Find All Unique Triplets that Sum up to Target
Find All Unique Triplets that Sum up to Target
Given an array of integers and a target sum, your task is to find all unique triplets in the array that add up to the target. A triplet is a group of three numbers.
Each triplet should be output in a Python-like tuple format and the collection of triplets should be represented as a list. Duplicate triplets are not allowed. The approach should aim for a time complexity close to \(O(n^2)\). Use appropriate algorithms such as the two-pointer technique after sorting the array.
inputFormat
The first line contains two space-separated integers, \(n\) and \(target\), where \(n\) is the number of elements in the array and \(target\) is the target sum.
The second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
If one or more unique triplets that sum up to \(target\) exist, output them in a Python list format. For example: [(-1, -1, 2), (-1, 0, 1)]
. Otherwise, output an empty list: []
.
6 0
-1 0 1 2 -1 -4
[(-1, -1, 2), (-1, 0, 1)]