DIY: Two Sum

Solve the interview question "Two Sum" yourself in this lesson.

We'll cover the following

Problem statement

In this challenge, you are given an array of integers called numbers and an integer called target. You are required to find the indices of two numbers that add up to the target value.

Note:

  • You cannot use the same element twice in the output. For example, if the value target/2 is present at the index i, then [i, i] is not a valid solution.
  • Also, you can assume that each array has only one solution.
  • The order of elements in the output does not matter.

Input

The function’s first input will be an array of integers called numbers and the second input will be an integer called target. The following is an example input:

[83, 97, 25]
108

Output

The function’s output will be a array of two integers representing the indices of the elements that sum to the target value. The following is the output for the inputs given above:

[0, 2]

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.