#K35392. Find Pair with Sum
Find Pair with Sum
Find Pair with Sum
You are given a list of integers and a target integer. Your task is to find a pair of numbers from the list that adds up to the given target. If such a pair exists, output the two numbers in the following format: [a, b]
. Otherwise, output an empty list []
.
Note that the pair should be output in the order in which they are found. The input is given via standard input, where the first line contains an integer n representing the number of elements in the list, the second line contains n space-separated integers, and the third line contains the target integer.
Input Format:
- The first line contains an integer n (n ≥ 0), the number of elements in the list.
- The second line contains n integers separated by spaces.
- The third line contains a single integer, the target value.
Output Format:
- If a valid pair is found, output it as
[a, b]
wherea
andb
are the two numbers. - If no such pair exists, output
[]
.
The solution should work for edge cases such as an empty list or when the list has only one element.
inputFormat
The input is read from standard input (stdin). The first line contains an integer n, the number of elements. The second line contains n space-separated integers. The third line contains the target integer.
outputFormat
Output the found pair in the format [a, b] if such a pair exists. Otherwise, output an empty pair as []. The output should be written to standard output (stdout).## sample
4
2 7 11 15
9
[2, 7]