#K44557. Find Pair with Sum

    ID: 27557 Type: Default 1000ms 256MiB

Find Pair with Sum

Find Pair with Sum

Given a sorted array of integers and a target sum \(T\), determine whether there exist two indices \(i\) and \(j\) (with \(i < j\)) such that \(A[i] + A[j] = T\). If such a pair exists, output the two indices in a single line separated by a space; otherwise, output -1.

You can solve this problem efficiently using the two-pointer technique, which yields a time complexity of \(O(n)\). The input array is guaranteed to be sorted in non-decreasing order.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer \(n\), representing the number of elements in the sorted array.
  • The second line contains \(n\) space-separated integers, which are the elements of the array.
  • The third line contains an integer \(T\), the target sum.

Note: The array uses 0-indexing.

outputFormat

The output is written to standard output (stdout). If there exists a pair of distinct elements whose sum equals \(T\), output the two indices separated by a space. Otherwise, output -1.

## sample
5
1 2 3 4 6
9
2 4