#include <iostream>
#include <vector>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (v[i] + v[j] == k)
cnt++;
}
}
cout << cnt;
return 0;
}
This code searches for all distinct pairs that their sum equals k then count how many distinct pairs are equal to k. Is there a way to do this task faster e.g. faster than O(n^2).