#C8652. Finding a Pair with Target Sum
Finding a Pair with Target Sum
Finding a Pair with Target Sum
Given an array of integers and a target value, your task is to determine if there exists a pair of distinct elements whose sum is equal to the target. If such a pair exists, output their indices (0-indexed) as a space-separated string. If no such pair exists, output -1.
You must read the input from stdin and print your answer to stdout.
The mathematical formulation for the problem is as follows:
Given an array \(a_0, a_1, \dots, a_{n-1}\) and a target value \(T\), find indices \(i\) and \(j\) (with \(i \neq j\)) such that:
\[ a_i + a_j = T \]If multiple solutions exist, output the first correct pair found; if none exists, output -1.
inputFormat
The input consists of two lines:
- The first line contains two integers \(n\) and \(T\), where \(n\) is the number of elements in the array and \(T\) is the target sum.
- The second line contains \(n\) space-separated integers representing the array elements.
outputFormat
If a valid pair is found, output the two indices (0-indexed) separated by a space. Otherwise, output -1
.
5 8
1 2 3 4 5
2 4