2 Sum

easyarrayshashing

You are given a list of integers. Your task is to identify any two distinct elements in the list whose sum is exactly equal to a given target value.

Let the array have $$$N$$$ integers where the $$$i$$$-th element has a value $$$a_i$$$.

You need to find two distinct indices $$$i$$$ and $$$j$$$ such that $$$a_i + a_j = T$$$, where $$$T$$$ is the target value.

Input Format:

The first line contains two integers $$$N$$$ and $$$T$$$ ($$$1 \leq N \leq 10^6$$$, $$$1 \leq T \leq 10^6$$$) — the number of elements and the target sum.

The second line contains $$$N$$$ integers $$$a_1, a_2, \dots, a_N$$$ ($$$1 \leq a_i \leq 10^6$$$) — the elements of the array.

Output Format:

Output two distinct integers $$$i$$$ and $$$j$$$ ($$$1 \leq i, j \leq N$$$, $$$i \ne j$$$), such that $$$a_i + a_j = T$$$. If there are multiple valid answers, print any. If there is no such pair, print $$$-1$$$.

Examples:

Example 1:

Input:

7 8
7 5 3 6 9 2 9

Output:

2 3

Example 2:

Input:

12 9
4 19 14 18 1 14 8 15 19 19 2 9

Output:

5 7

Note:

In the first example, elements at indices $$$2$$$ and $$$3$$$ have values $$$5$$$ and $$$3$$$, summing to $$$8$$$.

In the second example, elements at indices $$$5$$$ and $$$7$$$ have values $$$1$$$ and $$$8$$$, which add up to $$$9$$$.

Code

Loading Editor...