Create two_sum_problem.cpp

This commit is contained in:
Samiksha Mishra 2023-10-03 14:39:12 +05:30 committed by GitHub
parent 6376bf46af
commit 5f3f83e8d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View 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