Skip to main content

0001 - Two Sum (Easy)

https://leetcode.com/problems/two-sum/

Problem Statement

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly** one solution**, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

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

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

Constraints:

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

Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity?

Approach 1: Brute Force

Tthe goal is to find two numbers in an array that add up to a given target number. Since this is an easy problem, most of the time brute force solutions would work due to the loose constraints. However, this brute force solution gives O(n2)O(n^2) time complexity and O(1)O(1) space complexity since it involves checking all possible pairs of numbers in the array and seeing if they add up to the target.

Written by @wingkwong
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = (int) nums.size();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j] == target) {
return {i, j};
}
}
}
return {-1, -1};
}
};

Approach 2: Sorting and Two Pointer

Make a vector of pairs of the original vector elements and the indices of the elements in the original vector. Sort this new formed vector. Using two pointer approach, find the sum of the elements pointed by the left and right pointers. If this sum is less than the target increment the left pointer by one, if the sum is greater than the target decrement the right pointer by one, and if the sum is equal to the target return the positions of the elements in the original array. This solution gives O(nlogn)O(nlogn) time complexity and O(n)O(n) space complexity.

Written by @skoden
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = (int)(nums.size());
vector<pair<int, int>> pairs;
for (int i = 0; i < n; i++) {
pairs.push_back({nums[i], i});
}
sort(pairs.begin(), pairs.end());
int left = 0, right = n - 1;
vector<int> ans;
while (left < right) {
int sum = pairs[left].first + pairs[right].first;
if (sum == target) {
ans.push_back(pairs[left].second);
ans.push_back(pairs[right].second);
break;
} else if (sum < target)
left++;
else
right--;
}
return ans;
}
};

Approach 3: Hash Table

A better way to do it is using one-pass hash table approach. We iterate each element and insert it into the hash table. We also check if the complement already exists in the hash table or not. If so, we can return the answer immediately. This solution gives O(n)O(n) time complexity and O(n)O(n) space complexity.

Written by @wingkwong
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = (int) nums.size();
unordered_map<int, int> m;
for (int i = 0; i < n; i++) {
int complement = target - nums[i];
if (m.count(complement)) {
return {i, m[complement]};
}
m[nums[i]] = i;
}
return {-1, -1};
}
};