#C9179. Find Three Numbers with Given Sum
Find Three Numbers with Given Sum
Find Three Numbers with Given Sum
You are given an array of integers and two numbers N and X. The task is to determine whether there exists a triplet in the array such that the sum of the three numbers is exactly X.
Formally, given an array \(A\) of size \(N\) and a number \(X\), check if there exist indices \(i, j, k\) (with \(i < j < k\)) such that
\(A[i] + A[j] + A[k] = X\)
Output True
if such a triplet exists, otherwise output False
.
The intended solution should have a time complexity better than \(O(N^3)\), for example by sorting the array and using a two-pointer approach.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains two space-separated integers \(N\) and \(X\), where \(N\) is the number of elements in the array and \(X\) is the target sum.
- The second line contains \(N\) space-separated integers representing the array elements.
outputFormat
Output a single line to standard output (stdout) which is True
if there exists a triplet in the array whose sum is \(X\), otherwise output False
.
6 24
12 3 4 1 6 9
True