#K76527. Pair with Sum
Pair with Sum
Pair with Sum
Given an array of integers and a target sum, determine whether there exists a pair of distinct elements in the array whose sum equals the target. If such a pair exists, output YES
; otherwise, output NO
. The intended solution should be efficient enough to handle larger inputs. Mathematically, if the array is \(a_1, a_2, \dots, a_n\) and the target is \(T\), you need to decide if there exists indices \(i, j\) with \(i \neq j\) such that \(a_i + a_j = T\).
inputFormat
The input is given in three lines:
- The first line contains an integer \(n\), denoting the number of elements in the array.
- The second line contains \(n\) space-separated integers, representing the array elements.
- The third line contains an integer, representing the target sum \(T\).
outputFormat
Output a single line with YES
if there exists a pair of distinct elements whose sum equals the target; otherwise, output NO
.
5
1 2 3 4 5
9
YES