#C7396. Find Indices for Target Sum
Find Indices for Target Sum
Find Indices for Target Sum
Given an array of n integers and a target sum \(S\), your task is to determine whether there exist two distinct indices \(i\) and \(j\) (with 1-based indexing) such that \(a_i + a_j = S\). If such a pair exists, output the indices (in the order they are found using the input order). Otherwise, output -1.
The problem can be mathematically formulated as finding \(i, j\) with \(1 \leq i < j \leq n\) such that:
[ a_i + a_j = S ]
If multiple valid answers exist, output the one encountered first when scanning the array from left to right.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer \(n\) which is the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the elements of the array.
- The third line contains the target sum \(S\).
outputFormat
Output to standard output (stdout) a single line:
- If there exists a pair \(i, j\) such that \(a_i + a_j = S\), print the two 1-indexed positions separated by a space.
- If no such pair exists, print -1.
6
1 2 3 4 5 6
8
3 5