#K77862. Find a Pair with a Given Sum
Find a Pair with a Given Sum
Find a Pair with a Given Sum
You are given an array of integers and an integer target value T. Your task is to find any pair of numbers a and b from the array such that their sum satisfies the equation \(a + b = T\). If such a pair exists, output the pair in the order in which they are found (where the first number is the complement of the current value and the second is the current number). If no such pair exists, output an empty list represented by []
.
You should solve this problem using an efficient approach with a single pass through the array (for example, using a hash table), achieving an average time complexity of \(O(n)\). The input will be provided via standard input and the output should be printed to standard output.
inputFormat
The first line of input contains an integer \(n\) denoting the number of elements in the array. The second line contains \(n\) space-separated integers. The third line contains an integer \(T\), the target sum.
outputFormat
If a pair exists such that their sum equals \(T\), print the two numbers separated by a space on one line. If no such pair exists, print []
.
4
2 7 11 15
9
2 7