Design an algorithm to find all pairs of integers within an array which sum to
One easy and (time) efficient solution involves a hash map from integers to integers. This
algorithm works as follows:
1. Let be V be the specified value, and let A be the array.
2. Create a hash map, M, which will map from array elements to occurrence counts.
3. For each element A[i] in the array:
If V-A[i] is present in M, then print A[I] and V-A[I], M[V-A[i]] times.
If A[i] is present in M, increment M[A[i]], otherwise let M[A[i]] = 1.
This algorithm conveys the special cases such as identical pairs (V/2, V/2) and repeated values
Definition of Complement: If we’re trying to find a pair of numbers that sums to z, the complement
of x will be z - x (that is, the number that can be added to x to make z). For example,
if we’re trying to find a pair of numbers that sum to 12, the complement of –5 would be 17.
The Algorithm: Imagine we have the following sorted array: {-2 -1 0 3 5 6 7 9 13 14 }. Let first
point to the head of the array and last point to the end of the array. To find the complement
of first, we just move last backwards until we find it. If first + last < sum, then there is no complement
for first. We can therefore move first forward. We stop when first is greater than last.
Why must this find all complements for first? Because the array is sorted and we’re trying progressively
smaller numbers. When the sum of first and last is less than the sum, we know that
trying even smaller numbers (as last) won’t help us find a complement.
Why must this find all complements for last? Because all pairs must be made up of a first and
a specified value.
使用hash map:
1假设V为给定的值,A为给定的数组。
2创建hash map M,M将从数组元素映射到出现次数。
3对数组中的元素A[i]:
如果 V-A[i] 在M中,打印A[I] 和V-A[I], M[V-A[i]] 次.
如果A[i]在M中,增加M[A[i]],否则M[A[i]] = 1.
算法能处理例如两个元素相等以及重复元素的特殊例子。 (V/2, V/2) (e.g. V=7 and A=[3,4,3,4]).
SOLUTIONOne easy and (time) efficient solution involves a hash map from integers to integers. This
algorithm works as follows:
1. Let be V be the specified value, and let A be the array.
2. Create a hash map, M, which will map from array elements to occurrence counts.
3. For each element A[i] in the array:
If V-A[i] is present in M, then print A[I] and V-A[I], M[V-A[i]] times.
If A[i] is present in M, increment M[A[i]], otherwise let M[A[i]] = 1.
This algorithm conveys the special cases such as identical pairs (V/2, V/2) and repeated values
(e.g. V=7 and A=[3,4,3,4]).
另外一种解法:
补充的定义:如果要找出和为z的两个元素,x的补充就为z-x。
算法:假设数组已经是排好序的。让first指向数组头,last指向数组尾。如果 first + last < sum,表示first没有补充,first前移。如果 first + last > sum,last后移。如果 first + last == sum first++ last--. 当first比last大时停止移动。
Alternate SolutionDefinition of Complement: If we’re trying to find a pair of numbers that sums to z, the complement
of x will be z - x (that is, the number that can be added to x to make z). For example,
if we’re trying to find a pair of numbers that sum to 12, the complement of –5 would be 17.
The Algorithm: Imagine we have the following sorted array: {-2 -1 0 3 5 6 7 9 13 14 }. Let first
point to the head of the array and last point to the end of the array. To find the complement
of first, we just move last backwards until we find it. If first + last < sum, then there is no complement
for first. We can therefore move first forward. We stop when first is greater than last.
Why must this find all complements for first? Because the array is sorted and we’re trying progressively
smaller numbers. When the sum of first and last is less than the sum, we know that
trying even smaller numbers (as last) won’t help us find a complement.
Why must this find all complements for last? Because all pairs must be made up of a first and
a last. We’ve found all complements for first, therefore we’ve found all complements of last.
//Implementation: #include <iostream> #include <conio.h> #include <algorithm> using namespace std; void print_pairs(int * ptr, int num, int sum) { std::sort(ptr, ptr + num); int first = 0; int last = num - 1; while (first < last) { int s = ptr[first] + ptr[last]; if (s == sum) { cout << ptr[first] << “ “ << ptr[last] << endl; ++first; --last; } else { if (s < sum) { ++first; } else { --last; } } } }