#C8896. Triplet Sum Problem
Triplet Sum Problem
Triplet Sum Problem
Given an array of N integers and a target sum K, determine whether there exist three distinct elements ai, aj, ak such that:
$$a_i + a_j + a_k = K$$
You are required to use an efficient algorithm to solve the problem. If such a triplet exists, output YES
; otherwise, output NO
.
Example: For N = 5
, K = 10
, and arr = [1, 2, 4, 3, 6]
, the output should be YES
because 1 + 3 + 6 = 10
.
inputFormat
The first line contains two integers N
(the number of elements in the array) and K
(the target sum), separated by a space.
The second line contains N
space-separated integers representing the array arr
.
Note: It is guaranteed that N ≥ 1
.
outputFormat
Output a single line containing YES
if there exists any three distinct array elements whose sum equals K
, otherwise output NO
.
5 10
1 2 4 3 6
YES
</p>