Grokking the Coding Interview: Patterns for Coding Questions
Vote
0% completed
Solution: Two Sum
Problem Statement
Given an array of integers nums and an integer target, return 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: nums =
[3, 2, 4], target =6 - Expected Output:
[1, 2] - Justification:
nums[1] + nums[2]gives2 + 4which equals6.
- Input: nums =
-
Example 2:
- Input: nums =
[-1, -2, -3, -4, -5], target =-8 - Expected Output:
- Input: nums =
.....
.....
.....
Like the course? Get enrolled and start learning!
Arturo Calderón
· 3 years ago
There is an error in Example 3, in the body of the text. There, it says that the expected output should be [1, 3] for an input of [10, 15, 20, 25, 30, 35] and a target of 45. Indexes 1 and 3 correspond to 10 and 25 which add to 35, not 45. The correct answer is [1, 4]