#C14220. Unique Triplets Summing to Target

    ID: 43846 Type: Default 1000ms 256MiB

Unique Triplets Summing to Target

Unique Triplets Summing to Target

You are given an array of integers and a target integer. Your task is to find all unique triplets \( (a, b, c) \) such that:

\(a + b + c = T\)

Each triplet must be listed in non-decreasing order (i.e. \(a \le b \le c\)) and the list of triplets as a whole should be sorted in lexicographical order. Two triplets are considered the same if they contain the same three numbers.

If no such triplets exist, output a single line with [].

Example:

  • Input: [-1, 0, 1, 2, -1, -4] with target 0
  • Output:
    -1 -1 2
    -1 0 1
        

inputFormat

The input is given from standard input and consists of three lines:

  1. The first line contains an integer \(n\) \( (n \geq 0)\), representing the number of elements in the array.
  2. The second line contains \(n\) space-separated integers.
  3. The third line contains a single integer \(T\), the target sum.

outputFormat

Print each unique triplet in a new line with each number separated by a single space. The numbers in each triplet must be in non-decreasing order and the triplets must be output in lexicographical order.

If no triplets satisfy the condition, output a single line containing [].

## sample
6
-1 0 1 2 -1 -4
0
-1 -1 2

-1 0 1

</p>