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