Adrita

ds lab 8 task 3

Jul 28th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int m,a,i,sum,r,c=0;;
  6. cin>>m;
  7. if(m<=0||m>=100)
  8. {
  9. cout<<"Invalid input"<<endl;
  10. return 0;
  11. }
  12. unordered_map<int,int>mp;
  13. for(i=0; i<m; i++)
  14. {
  15. cin>>a;
  16. mp[a]++;
  17. }
  18. cin>>sum;
  19. for(auto j:mp)
  20. {
  21. if(j.second>0)
  22. {
  23. r=sum-j.first;
  24. if(mp.find(r)!=mp.end())
  25. {
  26. if(c==0)
  27. cout<<"Pairs with sum "<<sum<<" are:";
  28. cout<<" ("<<j.first<<","<<r<<") ";
  29. c=1;
  30. mp[r]=0;
  31. j.second=0;
  32. }
  33. }
  34. }
  35. if(c==0)
  36. cout<<"No pairs found"<<endl;
  37.  
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment