fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26. int consistency(int n, int k) {
  27.  
  28. unordered_map<int,int> mp;
  29. int ans = 0;
  30. for(int i=0; i<n; i++){
  31. if(mp.count(a[i] + k)){
  32. ans += mp[a[i] + k];
  33. }
  34. mp[a[i]]++;
  35. }
  36.  
  37. return ans;
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. int practice(int n, int k) {
  54.  
  55. int ans = 0;
  56.  
  57. unordered_map<int,int> mp;
  58.  
  59. for(int i=0; i<n; i++){
  60. if(mp.count(k+a[i])){
  61. ans += mp[k+a[i]];
  62. }
  63. mp[a[i]]++;
  64. }
  65.  
  66. return ans;
  67.  
  68. }
  69.  
  70. void solve() {
  71.  
  72. int n, k;
  73. cin >> n >> k;
  74. a.resize(n);
  75. for(int i=0; i<n; i++) cin >> a[i];
  76.  
  77. cout << consistency(n, k) << endl ;
  78.  
  79.  
  80. // cout << consistency(n, k) << " -> " << practice(n, k) << endl ;
  81.  
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. int32_t main() {
  89. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  90.  
  91. int t = 1;
  92. while (t--) {
  93. solve();
  94. }
  95.  
  96. return 0;
  97. }
Success #stdin #stdout 0s 5320KB
stdin
5 2
1 5 3 4 2
stdout
2