给定数组A []和数字X,请检查A []中是否有对X | 使用两个指针算法,O(1)空间复杂度| 套装2...
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=2Output: 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 foundWe 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=5Iteration 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 rightIteration 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--;elseleft++;}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";elsecout << "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
總結
以上是生活随笔為你收集整理的给定数组A []和数字X,请检查A []中是否有对X | 使用两个指针算法,O(1)空间复杂度| 套装2...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ruby 将日期转化为时间_Ruby中的
- 下一篇: mcq 队列_MCQ | 软件程序分析工