#K50402. 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 unsorted array of integers and a target sum \(T\). Your task is to find a pair of numbers in the array whose sum is equal to \(T\). If such a pair exists, output the pair in the order they are first encountered (i.e. the complement found from a previously seen number and the current number). If no such pair exists, output an empty array []
.
Example:
Input: 7 10 5 2 3 -6 9 11 4</p>Possible Output: [10, -6]
Note that if there are multiple valid answers, you can output any one of them. Use the following input format:
- The first line contains an integer \(N\) representing the number of elements in the array.
- The second line contains \(N\) integers separated by spaces.
- The third line contains an integer \(T\) representing the target sum.
inputFormat
The input is read from standard input (stdin) in the following format:
- The first line contains an integer \(N\), the number of elements in the array.
- The second line contains \(N\) integers separated by spaces denoting the array elements.
- The third line contains an integer \(T\) representing the target sum.
outputFormat
Output a pair of numbers enclosed in square brackets in the format [num1, num2]
if a valid pair is found. If no such pair exists, output []
. The output is written to standard output (stdout).
7
10 5 2 3 -6 9 11
4
[10, -6]