mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Create two_sum_problem.cpp
This commit is contained in:
parent
6376bf46af
commit
5f3f83e8d5
37
hashing/two_sum_problem.cpp
Normal file
37
hashing/two_sum_problem.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
// C++ program to check if given array
|
||||
// has 2 elements whose sum is equal
|
||||
// to the given value using hashing technique
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void print_pairs(int arr[], int arr_size, int sum)
|
||||
{
|
||||
unordered_set<int> s;
|
||||
for (int i = 0; i < arr_size; i++) {
|
||||
int temp = sum - arr[i];
|
||||
|
||||
if (s.find(temp) != s.end()) {
|
||||
cout << "Yes" << endl;
|
||||
return;
|
||||
}
|
||||
s.insert(arr[i]);
|
||||
}
|
||||
cout << "No" << endl;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main()
|
||||
{
|
||||
int A[] = { 1, 4, 45, 6, 10, 8 };
|
||||
int n = 16;
|
||||
int arr_size = sizeof(A) / sizeof(A[0]);
|
||||
|
||||
// Function calling
|
||||
print_pairs(A, arr_size, n);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Input: arr[] = {1, -2, 1, 0, 5}, x = 0
|
||||
// Output: No
|
Loading…
Reference in New Issue
Block a user