-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpair_sum.cpp
103 lines (84 loc) · 2.19 KB
/
pair_sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <unordered_set>
using namespace std;
/*
given unsorted array and a sum
find if there exists a pair of two array element such that their sum is equal to given sum
ip: arr[] = {3, 2, 15, -8, 8} sum = 17
op: Yes (2, 15)
ip: arr[] = {2, 4, 6, 3} sum = 11
op: No
ip: arr[] = {5, 8, -3, 6} sum = 3
op: Yes
sol 2: If array was sorted, we could have used two pointer approach which could be a O(nlogn)+O(n) sol
*/
/*
sol1: O(n^2) time, O(1) time
Traverse all els from L to R
for every el, traverse the els after it
check if sum of curr_el + anyother el == given sum
if it is return true
at the end if loop is over, return false
worst case: all subarrays are traversed, so n-1 + n-2...1
*/
bool hasPair(int arr[], int n, int sum)
{
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if(arr[i] + arr[j] == sum)
return true;
return false;
}
/*
sol3: O(n) time and space
traverse from L to R
at ith element, elements from 0 to i-1 will be in hashtable, not the complete array but only els from 0 to i-1
check if ith element, is forming a valid pair with any of the elements present in hashtable
we do this by searching for sum-arr[i] in hashtable
if it does not form a pair, insert the element in hashtable
*/
bool hasPair_hashing(int arr[], int n, int sum)
{
unordered_set<int> us;
for (int i = 0; i < n; i++)
{
if(us.find(sum-arr[i]) != us.end())
return true;
us.insert(arr[i]);
}
return false;
}
int main()
{
int a[] = {3, 2, 15, -8, 8};
int n = 5;
int sum = 17;
cout << hasPair(a, n, sum);
cout << hasPair_hashing(a, n, sum);
}
/*
op:
1 (true)
1 (true)
*/
/*
if asked to print indices of the pair
*/
class Solution {
public:
vector<int> twoSum(vector<int>& arr, int target) {
int n = arr.size();
vector<int> ans(2);
unordered_map<int, int> um;
for (int i = 0; i < n; i++)
{
if(um.find(target-arr[i]) != um.end())
{
ans[0] = um[target-arr[i]];
ans[1] = i;
}
um.insert({arr[i], i});
}
return ans;
}
};