#C9094. Unique Triplets with Given Sum
Unique Triplets with Given Sum
Unique Triplets with Given Sum
Given an array of integers \( A = [a_1, a_2, \ldots, a_n] \) and a target integer \( T \), find all unique triplets \( (a, b, c) \) such that:
$$a + b + c = T$$
The triplets in the output must satisfy the following conditions:
- Each triplet \( (a, b, c) \) is in non-decreasing order, i.e., \( a \leq b \leq c \).
- The overall list of triplets should be sorted in lexicographical order.
- No duplicate triplets are allowed even if the array contains duplicate numbers.
You are to implement an efficient algorithm that finds these triplets.
inputFormat
The input is provided via stdin in the following format:
- The first line contains an integer \( n \) representing the number of elements in the array.
- The second line contains \( n \) space-separated integers.
- The third line contains an integer \( T \) representing the target sum.
outputFormat
For each unique triplet that sums to the target, output a line with the three integers separated by a single space. The triplets must be printed in lexicographical order. If no valid triplet exists, output nothing.
## sample6
-1 0 1 2 -1 -4
0
-1 -1 2
-1 0 1
</p>