fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int count(int a[],int n,int k){
  4. int count=0;
  5. unordered_map<int,int>b;
  6. for(int i=0;i<n;i++){
  7. int x=k+a[i];
  8. int y=-k+a[i];
  9. if(b.find(x)!=b.end()){
  10. count+=b[x];
  11.  
  12. }
  13. if(b.find(y)!=b.end()){
  14. count+=b[y];
  15.  
  16. }
  17. b[a[i]]++;
  18. }
  19. return count;
  20.  
  21. }
  22.  
  23. int main() {
  24. // your code goes here
  25. int a[]={5,6,4,3,6};
  26. int n=sizeof(a)/sizeof(a[0]);
  27. int k=3;
  28. int ans=count(a,n,k);
  29. cout<<ans;
  30.  
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
2