#C12622. Triplet Sum Finder
Triplet Sum Finder
Triplet Sum Finder
You are given an array of integers and a target sum \(T\). Your task is to determine if there exist three distinct elements in the array such that their sum equals \(T\). This problem requires careful use of sorting and the two-pointer technique to achieve an optimal solution.
Problem Details:
- Given an integer \(n\) representing the size of the array.
- Followed by \(n\) space-separated integers.
- Followed by an integer \(T\), the target sum.
If there is any triplet in the array whose sum equals \(T = a_i + a_j + a_k\) (with \(i, j, k\) being distinct indices), output True
; otherwise, output False
.
Note: Use the efficient two-pointer technique after sorting the array to try and reduce the time complexity to \(O(n^2)\).
inputFormat
The input is read from stdin and is structured as follows:
- The first line contains a single integer \(n\), the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array elements.
- The third line contains a single integer \(T\), the target sum.
outputFormat
Output a single line to stdout with either True
or False
depending on whether there exists a triplet that sums to \(T\).
6
12 3 4 1 6 9
24
True
</p>