#C6473. Two Sum in a Sorted Array
Two Sum in a Sorted Array
Two Sum in a Sorted Array
Given a sorted array of distinct integers (A = [a_0, a_1, \dots, a_{n-1}]) and a target integer (T), find two indices (i) and (j) (with (i < j)) such that (a_i + a_j = T). If such a pair exists, output the indices separated by a single space. Otherwise, output -1.
The array is guaranteed to be sorted in ascending order and all elements are distinct. Use an efficient two-pointer approach to solve this problem in (O(n)) time.
inputFormat
The input is read from standard input (stdin). The first line contains an integer (n) representing the number of elements in the array. The second line contains (n) space-separated integers representing the sorted array. The third line contains a single integer (T) which is the target sum.
outputFormat
Output the indices of the two numbers that add up to the target, separated by a space. If no such pair exists, output -1. The output should be written to standard output (stdout).## sample
4
2 7 11 15
9
0 1