fork download
  1. /**
  2.  * author: orzvanh14 ( Độc cô cầu đặc )
  3.  * created: 18.04.2026 03:56:02
  4.  * too lazy to update time
  5. **/
  6. // i wants to take ioi
  7. //binhtinhtutinkhongcaycunhungmotkhikhongcontutinnualatuyetvong
  8. #include <bits/stdc++.h>
  9.  
  10. using namespace std;
  11.  
  12. #define int long long
  13. #define nn "\n"
  14. #define pi pair<int, int>
  15. #define ti tuple<int, int, int>
  16. #define fi first
  17. #define se second
  18. #define lb lower_bound
  19. #define ub upper_bound
  20. #define eb emplace_back
  21. #define pb push_back
  22. #define TASK " "
  23.  
  24. #define ms(a, x) memset(a, x, sizeof(a))
  25. #define all(a) a.begin(), a.end()
  26. #define All(a, n) a + 1, a + 1 + n
  27.  
  28. #define LOG 19
  29.  
  30. const int INF = 1e18;
  31. const int N = 1e5 + 5;
  32. const int maxn = 100 + 5;
  33. const int mod = 1e9 + 7;
  34.  
  35.  
  36. struct node{
  37. int kc, u;
  38. bool operator<(const node& other) const {
  39. return kc > other.kc;
  40. }
  41. };
  42. int n, k;
  43. int a[N];
  44. void nhap(){
  45. cin >> n >> k;
  46. for(int i = 1; i <= n; i++) cin >> a[i];
  47. }
  48. void solve(){
  49. map<int, int> cnt;
  50. int dem = 0;
  51. for(int i = 1; i <= n; i++){
  52. if(cnt[a[i] - k] > 0){
  53. dem++;
  54. }
  55. if(cnt[a[i] + k] > 0) dem++;
  56. cnt[a[i]]++;
  57. }
  58. cout << dem << nn;
  59. }
  60. signed main(){
  61. // freopen("wormsort.in", "r", stdin);
  62. // freopen("wormsort.out", "w", stdout);
  63. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  64. nhap();
  65. solve();
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0s 5324KB
stdin
5 2
1 5 3 4 2

stdout
3