#C857. Pair with Sum
Pair with Sum
Pair with Sum
Given an array of n integers and an integer target k, determine whether there exist two distinct elements in the array whose sum is exactly k. If such a pair exists, output YES
; otherwise, output NO
.
The problem can be formulated mathematically as follows: Find indices \(i\) and \(j\) with \(i \neq j\) such that \[ a_i + a_j = k, \] where \(a_1, a_2, \ldots, a_n\) are the elements of the array.
Note: The input is taken from the standard input and the result is printed to the standard output.
inputFormat
The first line contains two integers n
and k
separated by a space, where n
denotes the number of elements in the array and k
is the target sum.
The second line contains n
integers separated by spaces representing the elements of the array.
outputFormat
Output a single line containing either YES
if there exists a pair of distinct indices such that their corresponding array elements sum to k
, or NO
if no such pair exists.
5 9
2 7 11 15 1
YES