#C14631. Two Sum Problem
Two Sum Problem
Two Sum Problem
Problem Statement:
Given an array of integers \(a_1, a_2, \ldots, a_n\) and an integer target, your task is to find two distinct indices \(i\) and \(j\) (0-indexed) such that:
$$a_i + a_j = \text{target}$$
If such a pair exists, output the two indices separated by a space. If there is no such pair, output -1
. In the case of multiple valid solutions, you may output the first valid pair you find based on the order of processing.
Note: The indices must be different, i.e. \(i \neq j\).
inputFormat
Input Format:
- The first line contains a single integer \(n\), 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 an integer representing the target sum.
outputFormat
Output Format:
If a valid pair of indices exists, output the two indices (0-indexed) separated by a space. Otherwise, output -1
.
4
2 7 11 15
9
0 1