Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Two Sum

Easy
arrayhash-table

Given an array of integers nums and an integer target, return the indices of the two numbers that add up to target. Exactly one valid answer exists, and you may not use the same element twice. Return the indices in increasing order.

Input

  • Line 1: space-separated integers nums
  • Line 2: integer target

Output

  • The two indices, space-separated (smaller index first).
Example 1
Input:
2 7 11 15
9
Output:
0 1

nums[0] + nums[1] = 2 + 7 = 9.

Example 2
Input:
3 2 4
6
Output:
1 2

nums[1] + nums[2] = 2 + 4 = 6.

Constraints

  • 2 ≤ nums.length ≤ 10^4
  • -10^9 ≤ nums[i] ≤ 10^9
  • Only one valid answer exists.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.