fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int arr[]= {1, 5, 3, 4, 2};
  6. int n=sizeof(arr)/sizeof(arr[0]);
  7. int diff=2;
  8. int count=0;
  9. unordered_map<int, int> mp;
  10. for(int j=0;j<n;j++){
  11. if(mp.find(-diff+arr[j])!=mp.end())
  12. count=count+mp[-diff+arr[j]];
  13. if(mp.find(diff+arr[j])!=mp.end())
  14. count=count+mp[diff+arr[j]];
  15. mp[arr[j]]++;
  16. }
  17. cout<<count;
  18. }
  19.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
3