fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Find total number of pairs (i,j), i < j,
  5. // such that (nums[i] + nums[j]) %5 ==0
  6.  
  7. int main()
  8. {
  9. int n;
  10.  
  11. int res = 0;
  12.  
  13. cin >> n;
  14.  
  15. vector<int> ar;
  16.  
  17. for (int i = 0; i < n; i++)
  18. {
  19. int y;
  20. cin >> y;
  21. ar.push_back(y);
  22. }
  23.  
  24. unordered_map<int, int> mp;
  25.  
  26.  
  27. for (int j = 0; j < n; j++)
  28. {
  29. int real=ar[j]%5;
  30. int r=5-real;
  31. r=r%5; //when a[j]=0 '(real numbers)we search for 5 which doesn't exist
  32. if(mp.find(r)!=mp.end())
  33. {
  34. res+=mp[r];
  35. }
  36. mp[real]++;
  37. }
  38.  
  39. cout << res << endl;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 5316KB
stdin
6
2 2 5 0 3 3
stdout
5