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

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

  1. 哈希表的最差复杂度是n2_给定数组A []和数字X,请检查A []中是否有对X | 使用哈希O(n)时间复杂度| 套装1...

    哈希表的最差复杂度是n2 Prerequisite: 先决条件: Hashing data structure 散列数据结构 Problem statement: 问题陈述: Given an arr ...

  2. 11 旋转数组的最小数字

    题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个 ...

  3. [剑指offer]面试题8:旋转数组的最小数字

    面试题8:旋转数组的最小数字 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2 ...

  4. js选出给定数组中所有的偶数/奇数/n的倍数(filter)

    js选出给定数组中所有的偶数/奇数/n的倍数(filter) 1.选出给定数组中所有的偶数 用for function collect_all_even(collection) {let res = ...

  5. 苹果炉石传说显示无法连接服务器,《炉石传说》无法连接到战网解决方法 无法连接请检查网络连接如何快速解决...

    导 读 炉石传说无法连接请检查网络连接怎么办?无法连接到战网怎么解决?近期游戏进行了版本更新,在本次的更新中,有很多玩家都出现了各式各样的bug,其中最频繁的一个就是提示无法连接,请检查网络连接或者显 ...

  6. 炉石传说显示无法连接服务器失败,《炉石传说》无法连接到战网解决方法 无法连接请检查网络连接如何快速解决...

    炉石传说无法连接请检查网络连接怎么办?无法连接到战网怎么解决?近期游戏进行了版本更新,在本次的更新中,有很多玩家都出现了各式各样的bug,其中最频繁的一个就是提示无法连接,请检查网络连接或者显示无法连 ...

  7. 编程笔试(解析及代码实现):求不重复数字之和​​​​​​​给定一组整型数字,里面有且仅有两个数字值出现了一次,其他的数字都出现了两次。请写出程序求出这两个只出现了一次的数字之和

    编程笔试(解析及代码实现):求不重复数字之和给定一组整型数字,里面有且仅有两个数字值出现了一次,其他的数字都出现了两次.请写出程序求出这两个只出现了一次的数字之和 目录 题目描述 代码实现 题目描述 ...

  8. 剑指offer:给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,..,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]

    给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]. 不能使用除 ...

  9. PHP中的数组建必须为数字吗,PHP检查数组中缺少的数字

    PHP检查数组中可能缺少的数字,也可以理解为计算两个数组中元素的差集.那么这里我们就可以通过PHP中range和array_diff这两个函数来实现. 推荐参考学习:<PHP教程> 下面我 ...

最新文章

  1. 这本1900页的机器学习数学全书火了!完整版开放下载
  2. ZooKeeper 基本介绍
  3. 让你的英语口语妙语连珠的句子
  4. postgresql 创建用户_Liunx系统安装PostgreSQL数据库教程,值得程序员收藏pg安装教程
  5. oracle sga 4031,Oracle ORA-4031错误产生的原因详解
  6. Shell(13)——find
  7. 事实上着就是MAYA4.5完全手册插件篇的内容
  8. matlab计算频域动态性能指标,基于MATLAB自动控制系统时域频域分析与仿真.doc
  9. java中对象 引用的概念_java中的对象 方法 引用 等一些抽象的概念是什么意思呢?...
  10. ubuntu14.04 mysql5.6_ubuntu14.04编译安装mysql5.6.28
  11. java读mysql增量_在Java中检索MySQL自动增量
  12. Linux系统(二)常用命令、进程管理
  13. 海外资管业价格战有多疯狂?史上首个零费率基金横空出世
  14. jdbc 生成建表语句_记录一次TDH的inceptor导出建表语句和数据
  15. c语言设计一个自动阅卷功能,基于WEB的C语言编程题自动阅卷系统的设计与实现...
  16. 计算机组策略无法编辑,win7系统无法打开本地组策略编辑器的解决方法
  17. Mongodb数据库(上)
  18. MySQL int(M)数值类型中M值的意义
  19. 安装kubernetes dashboard时开发环境,运行gulp local-up-cluster任务一直显示wating for a heapster
  20. 读《枪炮,病菌和钢铁》

热门文章

  1. 表情符号mysql utf8mb4_mysql utf8mb4与emoji表情
  2. ubuntu下使用python将ppt转成图片_Ubuntu下使用Python实现游戏制作中的切分图片功能...
  3. c++ file* 句柄泄漏_C++核心指南:P.8 勿泄漏任务资源
  4. Angular 8之升级和新功能摘要
  5. C#浅拷贝与深拷贝区别
  6. IPv4地址分类及特征
  7. Office快捷键大全之三(Access快捷键下篇)
  8. unity, undo
  9. 页面访问的常见错误码解析
  10. ORA-00304: requested INSTANCE_NUMBER is busy