Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <stack>
- using namespace std;
- int main()
- {
- int n, k;
- cin >> n >> k;
- int niza[n];
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- }
- sort(niza, niza + n);
- long long parovi = 0;
- for(int i = 0; i < n; i++) {
- int indeks = lower_bound(niza, niza + n, niza[i] + k) - niza;
- int indeks2 = lower_bound(niza, niza + n, niza[i] + k + 1) - niza;
- if(indeks < n and niza[indeks] == niza[i] + k) {
- parovi += indeks2 - indeks;
- }
- }
- if(k == 0) {
- parovi -= n;
- parovi /= 2;
- }
- cout << parovi << endl;
- return 0;
- }
- /*
- 8 1
- 1 1 1 2 2 2 3 4
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement