Back to Solutions

Sum Of Two Values

Explanation

Here, we are given an array of n integers and we need to find 2 integers who sum is x.

This can be done by first storing all the indices of each element in a hashmap, then sorting the array and iterating through each element, checking whether x - arr[i] exists in the hashmap. If it does, then we can print the indices of the 2 elements.

Code

from collections import defaultdict
from sys import stdin

input = stdin.readline

n, x = map(int, input().split())
arr = list(map(int, input().split()))


indices = defaultdict(lambda: [])

for i, e in enumerate(arr):
    if x - e < 0:
        continue
    indices[e].append(i)

arr.sort()
for e in arr:
    if x - e < 0:
        continue
    if indices[x - e]:
        for i in indices[e]:
            for j in indices[x - e]:
                if i != j:
                    print(i + 1, j + 1)
                    exit()

print("IMPOSSIBLE")