Given an array of numbers, return indices of the two numbers which add up to a specific
total.
int
cha
si:
You may assume that each input would have exactly one solution, and you may not use the
same element twice.
che
for
CONSTRAINTS:
5-
6
7
8
9
10
11
12
13
14
15
16 -
17
18
19
20
21
1 <- T <= 1000
1 (N < 10000e
0 <- Arr[i] < 1000000
0 <- TARGET <= 1000000
int
//
wh
}
for
pr
ret
22 )
INPUT FORMAT:
The first line of input contains an integer denoting the number of
test-cases.
The first line of each test-case contains 2 space-separated integer
values N and TARGET, representing the size of array and the target sum
value
The second line of each test-case contains N space-separated integer
values.
OUTPUT FORMAT:
For each test case at new line print the indices of 2 numbers which add
upto TARGET values.
SAMPLE INPUT:
Answers
Answered by
0
Answer:
Explanation:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
for each number X in list of numbers:
for each number Y in list of numbers starting from X:
if X+Y equal target number, return indices
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
answer = []
Similar questions