C++ Algorithms to Find Pair of Sum Given a Collection of Numbers

  • 时间:2020-09-27 14:36:16
  • 分类:网络文摘
  • 阅读:122 次

This is quite similar to the Two Sum puzzle.

Given a collection of numbers, write a function that finds a pair that will sum to a given value. For example, the sum we are looking for is 10 and the collection may be [2, 0, 11, 9, 7] or [5, 2, 1, 5]. The function need only return a boolean on whether any pair within the collection satisfies the condition, so False and True for the first and second collection in the example, respectively. You can assume all numbers are integers.

Assume the collection is UNORDERED. Try to find a solution that minimizes the TIME complexity.

What is the time complexity of your solution? Use big-O notation.
What is the memory complexity of your solution? Use big-O notation.

The following is the C++ psue-do code. The idea is to use a hash set that reduces the lookup time to O(1). We need to take care of the duplicate numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// O(n) space and O(n) time
bool findPair(const vector<int> &nums, int sum) {
    unordered_set<int> hash;
    // enter all these numbers in a hash table
    for (const auto &n: nums) {
        if (!hash.count(n)) {
            hash.include(n);
        } else {
            if (sum - n == n) { // make sure duplicate number sums.
                return true;
            }
        }
    }
    for (const auto &n: nums) {
        // if the other is existent
        if (hash.count(sum - n)) {
            return true;
        }
    }
    return false;
}
// O(n) space and O(n) time
bool findPair(const vector<int> &nums, int sum) {
    unordered_set<int> hash;
    // enter all these numbers in a hash table
    for (const auto &n: nums) {
        if (!hash.count(n)) {
            hash.include(n);
        } else {
            if (sum - n == n) { // make sure duplicate number sums.
                return true;
            }
        }
    }
    for (const auto &n: nums) {
        // if the other is existent
        if (hash.count(sum - n)) {
            return true;
        }
    }
    return false;
}

Given a collection of numbers, write a function that finds a pair that will sum to a given value. For example, the sum we are looking for is 10 and the collection may be [2, 0, 11, 9, 7] or [5, 2, 1, 5]. The function need only return a boolean on whether any pair within the collection satisfies the condition, so False and True for the first and second collection in the example, respectively. You can assume all numbers are integers.

Assume the collection is ORDERED. Try to find a solution that minimizes the TIME AND MEMORY complexity.

What is the time complexity of your solution? Use big-O notation.
What is the memory complexity of your solution? Use big-O notation.

If the collection is sorted, we can use two pointers starting at both ends, moving towards each other depending on the sum comparison.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// O(N) - time, and O(1) space
bool findPair(const vector<int> nums, int sum) {
    auto i = 0;
    auto j = nums.size() - 1;
    while (i < j) {
        if (nums[i] + nums[j] == sum)    {
            return true;
        }
        if (arr[i] + arr[j] < sum) {
            i ++;
        } else {
            j --;
        }
    }
    return false;
}
// O(N) - time, and O(1) space
bool findPair(const vector<int> nums, int sum) {
    auto i = 0;
    auto j = nums.size() - 1;
    while (i < j) {
        if (nums[i] + nums[j] == sum)    {
            return true;
        }
        if (arr[i] + arr[j] < sum) {
            i ++;
        } else {
            j --;
        }
    }
    return false;
}

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
好时巧克力1年3上黑榜 违规使用维生素E  近些年被曝光的大米安全事件一览  夏日进补:男不离韭,女不离藕之解读  “僵尸肉”蹿上餐桌拷问食品安全监管  夏季炒菜时可多放一种调料——花椒  四种热门食物并没有那么神奇的营养功效  “养胃饼干”不养胃 消费者起诉徐静蕾代言  盛夏常饮枸杞菊花茶 保养眼睛功效强  西瓜虽好,但这九类人群不宜吃西瓜  保健酒乱象调查:为见效快违法添加伟哥 
评论列表
添加评论