Two Sum

Asked atAdobePluangZZupeeTTarget
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Given an array of integers **nums and an integer target. Return the indices(0 - indexed) of two elements in nums **such that they add up to target.

Each input will have exactly one** **solution, and the same element **cannot be used twice. Return **the answer in any order.

Input: nums = [1, 6, 2, 10, 3], target = 7 Output: [0, 1] Explanation: nums[0] + nums[1] = 1 + 6 = 7

Input: nums = [1, 3, 5, -7, 6, -3], target = 0 Output: [1, 5] Explanation: nums[1] + nums[5] = 3 + (-3) = 0

Input: nums = [-6, 7, 1, -7, 6, 2], target = 3

  • 2 <= nums.length <= 105
  • -104 <= nums[i] <= 104
  • -105 <= target <= 105
  • Only one valid answer exists.

cpp

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        
    }
};

java

class Solution {
    public int[] twoSum(int[] nums, int target) {
        
    }
}

python

class Solution:
    def twoSum(self, nums, target):

javascript

class Solution {
    twoSum(nums, target) {

    }
}

csharp

public class Solution {
    public List<int> TwoSum(List<int> nums, int target) {
        
    }
}

go

func twoSum(nums []int, target int) []int {
    //your code goes here
}
Solve on LeetCode →
Stuck? Show a way to structure it+
  1. 01For each value x, compute the needed complement target - x.
  2. 02Check whether that complement was seen at an earlier index.
  3. 03Store the current value and index only after the lookup.
  4. 04Return original indices and ensure one element is never used twice.

Reference answer

Then expect these follow-ups

  • How would you return every unique pair instead of one guaranteed pair?

    Tests: duplicate handling

  • What changes if the input is already sorted?

    Tests: algorithm choice

Hiring now

Adobe has 203 open roles right now

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • 2027 Intern - Solutions Consulting

    Paris

  • 2027 Intern - Communications

    San Francisco

  • Adobe Intern FY27

    Bangalore

Or practice this question first

Next question