#C2013. Pair with Given Sum
Pair with Given Sum
Pair with Given Sum
You are given an array A of n integers and a target integer k. Your task is to determine whether there exist two distinct elements in the array such that their sum is equal to k. In other words, find two indices i and j (with i \neq j) for which
\(A_i + A_j = k\)
If such a pair exists, print YES
; otherwise, print NO
.
The problem requires an efficient solution. Typical approaches involve the use of a hash set or sorting with two pointers.
inputFormat
The input is given via standard input (stdin) and consists of:
- An integer
n
, the number of elements in the array. - A line with
n
space-separated integers representing the array A. - An integer
k
on a new line, the target sum.
outputFormat
Output a single line to standard output (stdout) containing either YES
if there exists a pair such that their sum is equal to k
, or NO
otherwise.
5
1 3 4 5 6
9
YES