#C6174. Find Three Numbers Summing to Target
Find Three Numbers Summing to Target
Find Three Numbers Summing to Target
Given an array of n integers and a target sum X, determine if there exist three distinct integers in the array whose sum equals X. It is guaranteed that each chosen integer comes from a different position in the array.
You are expected to implement a solution with efficient time complexity. A common approach is to sort the array and then use the two-pointer technique to check for a valid triplet. Specifically, for each element, try to find a pair in the remaining part of the array such that their sum along with the current element equals X.
The formula for the target condition is given below:
\(a_i + a_j + a_k = X\)
If such a triplet exists, print Yes
; otherwise, print No
.
inputFormat
The input is given from stdin and consists of three parts:
- 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 X, the target sum.
outputFormat
Output a single line to stdout with Yes
if there exist three distinct integers in the array that sum to X, otherwise output No
.
5
1 2 3 4 5
9
Yes