-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountNumber.cpp
More file actions
59 lines (54 loc) · 1.32 KB
/
CountNumber.cpp
File metadata and controls
59 lines (54 loc) · 1.32 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
58
59
/*
Problem: Count number of pairs of sequence of distinct integer sum equal to Q
Description
Given a sequence of distinct integers a_1, a_2, ..., a_n and an integer Q. Count number M of pairs (i, j)
such that 1 ≤ i< j ≤ n, and a_i + a_j = Q
Input
•Line 1: contains two integers nand Q
•Line 2: contains a_1, a_2, ..., a_n
Ouput
•Write the value of M
Example
Input
5 8
4 6 5 3 2
Output
2
*/
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n, Q;
cin >> n >> Q;
vector<int> a;
a.resize(n+1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int result = 0;
// O(n^2) approach
// for (int i = 1; i <= n; i++)
// {
// for (int j = i+1; j <= n; j++)
// {
// if (a[i] + a[j] == Q) result++;
// }
// }
// --------------
// O(n) approach
unordered_set<int> set; // lưu trữ các số
unordered_set<int> seen; // lưu trữ các pair mà đã gặp trước đó
for(int i = 1; i <= n; i++) {
int num = a[i];
if (set.find(Q-num)!=set.end() && seen.find(num) == seen.end())
{
result++;
seen.insert(Q-num);
seen.insert(num);
}
set.insert(num);
}
cout << result << endl;
return 0;
}