#K60822. Pair With Sum
Pair With Sum
Pair With Sum
You are given an integer n
, a target integer k
, and a list of n
integers. Your task is to determine whether there exists a pair of distinct elements in the list whose sum is exactly k
. If such a pair exists, output True
; otherwise, output False
.
The pair must consist of two different indices. Note that the same number may appear multiple times in the list. However, you cannot use the same element twice unless it appears at least twice in the list.
Hint: An efficient approach is to use a hash set (or similar data structure) to check, for each element, if the difference k - element has already been seen.
inputFormat
The input is given from stdin
in the following format:
n k a1 a2 ... an
Where n
is the number of integers, k
is the target sum, and the second line contains n
space-separated integers.
outputFormat
Print a single line to stdout
with either True
or False
indicating whether there exists a pair of distinct elements whose sum equals k
.
5 9
1 3 5 7 8
True