Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Two Sum (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

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

  1. Example 1:

    • Input: nums = [3, 2, 4], target = 6
    • Expected Output: [1, 2]
    • Justification: nums[1] + nums[2] gives 2 + 4 which equals 6.
  2. Example 2:

    • Input: nums = [-1, -2, -3, -4, -5], target = -8
    • Expected Output: [2, 4]
    • Justification: nums[2] + nums[4] yields -3 + (-5) which equals -8.
  3. Example 3:

    • Input: nums = [10, 15, 21, 25, 30], target = 45
    • Expected Output: [1, 4]
    • Justification: nums[1] + nums[4] gives 15 + 30 which equals 45.

Constraints:

  • 2 <= nums.length <= 10<sup>4</sup>
  • -10<sup>9</sup> <= nums[i] <= 10<sup>9</sup>
  • -10<sup>9</sup> <= target <= 10<sup>9</sup>
  • Only one valid answer exists.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible