-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairEqualsToSum.cpp
More file actions
57 lines (53 loc) · 1.14 KB
/
pairEqualsToSum.cpp
File metadata and controls
57 lines (53 loc) · 1.14 KB
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
/*Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K.
Example 1:
Input:
N = 4, K = 6
arr[] = {1, 5, 7, 1}
Output: 2
Explanation:
arr[0] + arr[1] = 1 + 5 = 6
and arr[1] + arr[3] = 5 + 1 = 6.
Example 2:
Input:
N = 4, X = 2
arr[] = {1, 1, 1, 1}
Output: 6
Explanation:
Each 1 will produce sum 2 with any 1. */
//Brute-Force O(n^2)
class Solution{
public:
int getPairsCount(int arr[], int n, int k) {
int c=0,temp[n];
for(int i=0;i<n;i++)
temp[i]=arr[i];
for(int i=0;i<n;i++)
{
if(arr[i])
}
return c;
}
};
//Optimal O(n)
class Solution{
public:
int getPairsCount(int a[], int n, int k) {
//map to store frequency of current element
unordered_map<int,int> m;
int x,c=0;
for(int i=0;i<n;i++)
{
x=k-a[i];
if(m[x]==0)
{
m[a[i]]++;
}
else
{
c+=m[x];
m[a[i]]++;
}
}
return c;
}
};