fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int countPairs(int arr[], int n, int target) {
  5. int cnt=0;
  6. unordered_map<int,int> mp;
  7. for(int i=0;i<n;i++){
  8. int complement = target - arr[i];
  9. if(mp.find(complement)!=mp.end()){
  10. cnt+=mp[complement];
  11. }
  12. mp[arr[i]]++;
  13. }
  14. return cnt;
  15. }
  16. int main() {
  17. // your code goes here
  18. int arr[5] = {1, 5, 7, -1, 5};
  19. int target=6;
  20. cout<<countPairs(arr,5,target);
  21. return 0;
  22. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
3