fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int count(vector<int>&arr , int k){
  5. unordered_map<int,int>hmap;
  6. int cnt=0;
  7. for(int i=0;i<arr.size();i++){
  8.  
  9.  
  10. if(hmap.find(k-arr[i])!=hmap.end()){
  11. cnt+=hmap[k-arr[i]];
  12. }
  13. hmap[arr[i]]++;
  14.  
  15. }
  16. return cnt;
  17. }
  18. int main(){
  19. vector<int> arr={1,2,3,4,5,5};
  20. int k=6;
  21.  
  22. cout<<count(arr,k);
  23.  
  24. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
3