Two Sum (easy)
Problem Statement
You are given an array of integers nums
and an integer target
. Your task is to find two distinct indices i
and j
such that the sum of nums[i]
and nums[j]
is equal to the target
. You can assume that each input will have exactly one solution, and you may not use the same element twice.
Examples
-
Example 1:
- Input:
[3, 2, 4]
,6
- Expected Output:
[1, 2]
- Justification:
nums[1] + nums[2]
gives2 + 4
which equals6
.
- Input:
-
Example 2:
- Input:
[-1, -2, -3, -4, -5]
,-8
- Expected Output:
[2, 4]
- Justification:
nums[2] + nums[4]
yields-3 + (-5)
which equals-8
.
- Input:
-
Example 3:
- Input:
[10, 15, 20, 25, 30]
,45
- Expected Output:
[1, 3]
- Justification:
nums[1] + nums[3]
gives15 + 30
which equals45
.
- Input:
Try it yourself
Try solving this question here:
Python3
Python3
. . .