Advertisement
Josif_tepe

Untitled

Dec 16th, 2022
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <stack>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int n, k;
  10.     cin >> n >> k;
  11.     int niza[n];
  12.    
  13.     for(int i = 0; i < n; i++) {
  14.         cin >> niza[i];
  15.     }
  16.     sort(niza, niza + n);
  17.     long long parovi = 0;
  18.     for(int i = 0; i < n; i++) {
  19.         int indeks = lower_bound(niza, niza + n, niza[i] + k) - niza;
  20.         int indeks2 = lower_bound(niza, niza + n, niza[i] + k + 1) - niza;
  21.        
  22.         if(indeks < n and niza[indeks] == niza[i] + k) {
  23.             parovi += indeks2 - indeks;
  24.         }
  25.        
  26.     }
  27.     if(k == 0) {
  28.         parovi -= n;
  29.         parovi /= 2;
  30.     }
  31.     cout << parovi << endl;
  32.     return 0;
  33. }
  34. /*
  35. 8 1
  36.  1 1 1 2 2 2 3 4
  37. */
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement