#C13998. Find Pair with Given Sum

    ID: 43597 Type: Default 1000ms 256MiB

Find Pair with Given Sum

Find Pair with Given Sum

Given an array of integers and a target sum, your task is to find two distinct elements whose sum equals the target. More formally, given an array (a) of (n) integers and an integer (target), find indices (i) and (j) (with (i \neq j)) such that (a_i + a_j = target). If such a pair exists, print the two indices (zero-based) separated by a space. In the event that multiple valid pairs exist, output the pair that is encountered first when scanning the array from left to right. If no such pair exists, output -1.

Example: For the array [10, 15, 3, 7] with target 17, one valid answer is "0 3" since (10 + 7 = 17).

inputFormat

The input is provided via standard input (stdin) in the following format:

  1. The first line contains an integer (n), representing the number of elements in the array.
  2. The second line contains (n) space-separated integers, which are the elements of the array. (If (n = 0), this line will be empty.)
  3. The third line contains an integer representing the (target) sum.

outputFormat

Output a single line to standard output (stdout). If a valid pair of indices is found, print the two indices separated by a space. If no such pair exists, print -1.## sample

4
10 15 3 7
17
0 3