-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKdiffPairs.cpp
More file actions
50 lines (46 loc) · 764 Bytes
/
Copy pathKdiffPairs.cpp
File metadata and controls
50 lines (46 loc) · 764 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int findPairs(vector<int> &nums, int k)
{
unordered_map<int, int> h;
for (int num : nums)
{
h[num]++;
}
int count = 0;
if (k != 0)
{
for (int num : nums)
{
if (h[num + k] > 0)
{
h[num + k] = 0;
count++;
}
}
}
else
{
for (int num : nums)
{
if (h[num] > 1)
{
h[num] = 0;
count++;
}
}
}
return count;
}
int main()
{
int n, k;
cin >> n >> k;
vector<int> v(n, 0);
for (int &i : v)
{
cin >> i;
}
cout << findPairs(v, k) << "\n";
return 0;
}