日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

给定数组A []和数字X,请检查A []中是否有对X | 使用两个指针算法,O(1)空间复杂度| 套装2...

發(fā)布時間:2025/3/11 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 给定数组A []和数字X,请检查A []中是否有对X | 使用两个指针算法,O(1)空间复杂度| 套装2... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Prerequisite:

先決條件:

  • Hashing data structure

    散列數(shù)據(jù)結(jié)構(gòu)

  • Given an array A[] and number X, check for pair in A[] with sum X | using hashing O(n) time complexity | Set 1

    給定數(shù)組A []和數(shù)字X,請檢查A []中是否有對X | 使用哈希O(n)時間復(fù)雜度| 套裝1

Problem statement:

問題陳述:

Given an array and a sum X, fins any pair which sums to X without using additional space.

給定一個數(shù)組和一個總和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中 ,我們看到了如何使用蠻力和哈希來解決這個問題。 我們還發(fā)現(xiàn),由于創(chuàng)建了其他哈希表,該算法具有額外的空間復(fù)雜度O(n)。 在本節(jié)中,我們將討論如何在不使用任何其他空間復(fù)雜性的恒定空間中解決此問題。

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.

想法是使用標(biāo)準(zhǔn)的兩指針?biāo)惴?#xff0c;其中我們在數(shù)組的兩端保留兩個指針,并根據(jù)使用的邏輯遍歷指針。

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:

為了使用兩個指針?biāo)惴ń鉀Q此問題,我們要求對輸入數(shù)組進(jìn)行排序。 由于輸入數(shù)組也可以不排序,因此我們需要對輸入數(shù)組進(jìn)行排序作為必要步驟,然后可以執(zhí)行以下指向指針的算法:

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:

我們可以使用我們的示例執(zhí)行上述算法:

[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 ++實現(xiàn):

#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

總結(jié)

以上是生活随笔為你收集整理的给定数组A []和数字X,请检查A []中是否有对X | 使用两个指针算法,O(1)空间复杂度| 套装2...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。