Prerequisite:
先决条件:
Hashing data structure
散列数据结构
Given an array A[] and number X, check for pair in A[] with sum X | using hashing O(n) time complexity | Set 1
给定数组A []和数字X,请检查A []中是否有对X | 使用哈希O(n)时间复杂度| 套装1
Problem statement:
问题陈述:
Given an array and a sum X, fins any pair which sums to X without using additional space.
给定一个数组和一个总和X ,对任何总和为X的对取整而不使用额外的空间。
Example:
例:
Input array:
[4, -1, 6, 5, -2, 2]
Sum, X=2
Output:
Pair {4,-2}
Explanation:
4+(-2)=2 and thus the pair is {4,-2}
Solution:
解:
In the set 1, we saw how to solve this problem using brute force and using hashing. We also found that due to additional hash table creation the algorithm has additional space complexity O(n). In this section, we will discuss how to solve this in constant space that is without using any additional space complexity.
在集合1中 ,我们看到了如何使用蛮力和哈希来解决这个问题。 我们还发现,由于创建了其他哈希表,该算法具有额外的空间复杂度O(n)。 在本节中,我们将讨论如何在不使用任何其他空间复杂性的恒定空间中解决此问题。
The idea is to use the standard two-pointer algorithm where we keep two pointers at two ends of the array and based on the logic used, we traverse the pointers.
想法是使用标准的两指针算法,其中我们在数组的两端保留两个指针,并根据使用的逻辑遍历指针。
To solve this problem using two pointer algorithm, we require the input array to be sorted. Since the input array can be unsorted as well, we require to sort the input array as a pre-requisite step and then can perform the below to pointer algorithm:
为了使用两个指针算法解决此问题,我们要求对输入数组进行排序。 由于输入数组也可以不排序,因此我们需要对输入数组进行排序作为必要步骤,然后可以执行以下指向指针的算法:
1) Initially set left pointer to the left end, i.e., left=0
2) Initially set right pointer to the right end, i.e., right=n-1, where n be the size of the array
3) While left<rightIf arr[left] + arr[right]==XWe have find the pair { arr[left], arr[right]}Else if arr[left] + arr[right]<XIncrement left as current sum is less than X(that's why we need sorted array)Else // if arr[left] + arr[right]>XDecrement right as current sum is more than X(that's why we need sorted array)End While
4) If not returned in the loop then there is no pair found
We can perform the above algorithm, using our example:
我们可以使用我们的示例执行上述算法:
[4, -1, 6, 5, -2, 2]
X=2
After sorting:
[-2,-1, 2, 4, 5, 6]
Initially:
Left=0
Right=5
Iteration 1:
Left<right
arr[left]+arr[right]=4
So current sum>X
So decrement right
Iteration 2:
Left=0
Right=4
Left<right
arr[left]+arr[right]=3
So current sum>X
So decrement right
Iteration 3:
Left=0
Right=3
Left<right
arr[left]+arr[right]=2
So current sum==X
Thus return { arr[left], arr[right]}
C++ implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
pair<int, int> find_pair_sum(vector<int> arr, int n, int X)
{
//sort the array takes O(logn)
sort(arr.begin(), arr.end());
int left = 0, right = arr.size() - 1;
while (left < right) {
if (arr[left] + arr[right] == X)
return make_pair(arr[left], arr[right]);
else if (arr[left] + arr[right] > X)
right--;
else
left++;
}
return make_pair(INT_MAX, INT_MAX);
}
int main()
{
cout << "Enter number of input elements,n\n";
int n;
cin >> n;
cout << "Enter the input elements\n";
vector<int> arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Enter sum, X\n";
int X;
cin >> X;
pair<int, int> p = find_pair_sum(arr, n, X);
if (p.first == INT_MAX && p.second == INT_MAX)
cout << "No pairs found\n";
else
cout << "The pairs are : " << p.first << ", " << p.second << endl;
return 0;
}
Output:
输出:
Enter number of input elements, n
6
Enter the input elements
4 -1 6 5 -2 2
Enter sum, X
2
The pairs are : -2, 4
翻译自: https://www.includehelp.com/data-structure-tutorial/given-an-array-a-and-number-x-check-for-pair-in-a-with-sum-x-set-2.aspx