Two Sum Indices
Python
Medium
5 views
Problem Description
For each testcase you get n, target and n integers. Output first pair indices i j (0-based) such that a[i]+a[j]=target. If not found output -1 -1.
Input Format
First integer t. For each: n target then n integers.
Output Format
t lines: i j.
Sample Test Case
Input:
2
5 9
2 7 11 15 1
3 10
1 2 3
Constraints
Sum of n over all testcases
Official Solution
import sys
p=sys.stdin.read().strip().split()
if not p:
sys.exit(0)
it=iter(p)
t=int(next(it))
out=[]
for _ in range(t):
n=int(next(it)); target=int(next(it))
mp={}
ans='-1 -1'
for i in range(n):
x=int(next(it))
y=target-x
if ans=='-1 -1' and y in mp:
ans=f'{mp[y]} {i}'
if x not in mp:
mp[x]=i
out.append(ans)
sys.stdout.write('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!