Question: Two Sum Given a list of integers and an integer target, write a function that takes the list and the target and returns a new

Two Sum

Given a list of integers and an integer target, write a function that takes the list and the target and returns a new list of indices of any two numbers in the list such that they add up to target.

You may assume that each input will have exactly one solution, and you are not allowed to use the same element at a given index twice.

You may return the list with indices in any order.

Example 1:

Input: numbers = [1, 2, 5, 4, 10], target = 15

Output: [2, 4]

Explanation: numbers[2] = 5 and numbers[4] = 10, which can be added to the target integer, 15.[4, 2] is also acceptable.

Example 2:

Input: numbers = [1, 1] , target = 2

Output: [0, 1]

Example:

Input: numbers = [1, 2, 3], target = 5

Output: [1, 2]

Explanation: numbers[1] + numbers[2] = 2 + 3 = 5

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!