Codelet Keep code simple stupid

Two Sum

Question

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

Solution

  • Brute Force (binary search)
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
  struct Item {
    int index;
    int value;

    bool operator<(const Item& a) const {
      return value < a.value;
    }
  };

  vector<int> twoSum(vector<int>& nums, int target) {
    // put nums into an temp array with index stored
    vector<Item> temp(nums.size());
    for (int i = 0; i < nums.size(); i++) {
      Item item;
      item.index = i;
      item.value = nums[i];
      temp[i] = item;
    }
    // sort the array
    sort(temp.begin(), temp.end());
    // find result indexes
    vector<int> result(2);
    for (int i = 0; i < temp.size(); i++) {
      Item dest;
      dest.value = target - temp[i].value;
      // find dest number by binary search
      pair<vector<Item>::iterator, vector<Item>::iterator> range;
      range = equal_range(temp.begin() + i + 1, temp.end(), dest);
      vector<Item>::iterator lower = range.first;
      vector<Item>::iterator upper = range.second;
      if (lower != upper) {
        result[0] = temp[i].index + 1;  // non zero-based
        result[1] = lower->index + 1;
        if (result[0] > result[1]) {
          swap(result[0], result[1]);  // make result ordered
        }
        break;
      }
    }
    return result;
  }
};
  • Two Pointers Search
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
  struct Item {
    int index;
    int value;

    bool operator<(const Item& a) const {
      return value < a.value;
    }
  };

  vector<int> twoSum(vector<int>& nums, int target) {
    // put nums into an temp array with index stored
    vector<Item> temp(nums.size());
    for (int i = 0; i < nums.size(); i++) {
      Item item;
      item.index = i;
      item.value = nums[i];
      temp[i] = item;
    }
    // sort the array
    sort(temp.begin(), temp.end());
    // two pointer search
    int left = 0;
    int right = temp.size() - 1;
    int sum = temp[left].value + temp[right].value;
    while ((sum != target) && (left < right)) {
      if (sum < target) {
        left++;
      } else {
        right--;
      }
      sum = temp[left].value + temp[right].value;
    }
    // result founded when sum == target
    vector<int> result(2);
    result[0] = temp[left].index + 1;  // non zero-based
    result[1] = temp[right].index + 1;
    if (result[0] > result[1]) {  // make result ordered
      swap(result[0], result[1]);
    }
    return result;
  }
};