All problems

Problem 2

Two Sum

Easy · 10 pts · Array, Hash Table

Problem

You are given an array of N integers and a target value. Find two different positions i and j (i < j) such that array[i] + array[j] equals the target, and print them using zero-based indexing. If several pairs work, print the one that is completed first when you scan the array from left to right: the pair with the smallest j, and among those, the smallest i. If no pair adds up to the target, print -1. The two positions must be different, but the values at them may be equal. This is exactly what the classic one-pass solution finds: walk through the array, and for each position j check whether target - array[j] appeared earlier, using a hash map from each value to the first index where it was seen.

Input

First line: integer N. Second line: N space-separated integers. Third line: target.

Output

Print the two indices i and j separated by a space, or -1 if no pair exists. Use zero-based indexing.

Constraints

2 <= N <= 100000; -1000000000 <= array[i], target <= 1000000000

Example input

4
2 7 11 15
9

Example output

0 1

Explanation

The array is [2, 7, 11, 15] and the target is 9. At j = 1 the value 7 needs a partner 9 - 7 = 2, which was seen at index 0. That is the first completed pair, so the answer is 0 1.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Add Two Numbers Next: Reverse Array