fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include <bits/stdc++.h>
  4. int main() {
  5. // your code goes here
  6. int n;
  7. cin>>n;
  8. int arr[n];
  9. int i=0;
  10. while(i<n)
  11. {
  12. cin>>arr[i];
  13. i++;
  14. }
  15.  
  16.  
  17. //int count=0;
  18. unordered_map<int,int>freq;
  19.  
  20. for(int k=2;k<=n;k++)
  21. {
  22. for(int l=k+1;k<=n;k++)
  23. {
  24. int sum=arr[k]+arr[l];
  25. freq[sum]++;
  26. }
  27. }
  28.  
  29.  
  30. int count=0;
  31.  
  32. for(int i=0;i<n;i++)
  33. {
  34. for(int j=i+1;j<n;j++)
  35. {
  36. int x = arr[i]+arr[j];
  37. count+=freq[-x];
  38. }
  39. }
  40. cout<<count;
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 5288KB
stdin
7
1 2 3 4 -1 -2 -2
stdout
2