Two Sum - aloalgo

Two Sum

Easy

You are given an integer array nums and an integer target.

Your task is to find two distinct indices i and j such that nums[i] + nums[j] = target.

You may assume that:

  • Exactly one valid pair of indices exists for each input.
  • The same array element cannot be used twice.
  • The indices may be returned in any order.

Return the two indices [i, j].

Example 1

Inputs
nums = [1, 2, 3, 5]
target = 5
Output
[1, 2]
Explanation:

Because nums[1] + nums[2] == 5, we return [1, 2].

Example 2

Inputs
nums = [3, 2, 4]
target = 6
Output
[1, 2]
Explanation:

Because nums[1] + nums[2] == 6, we return [1, 2].

Example 3

Inputs
nums = [2, 2, 1]
target = 4
Output
[0, 1]
Explanation:

Because nums[0] + nums[1] == 4, we return [0, 1].

Loading...
Inputs
nums = [1, 2, 3, 5]
target = 5
Output
[1, 2]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!