fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using ll = long long int;
  4. #define fo(i,start,end) for(int i=start;i<end;i++)
  5. using namespace std;
  6.  
  7. int main() {
  8. // your code goes here
  9. int arr[] = {3,2,1,2,5};
  10. int k = 4;
  11. int n = 5;
  12. unordered_map<ll,ll>mp;
  13. //store the element and its frequency
  14. fo(i,0,n){
  15. int complement = k - arr[i];
  16. if(mp.find(complement) != mp.end()){
  17. //element is present
  18. //return the pair
  19. auto it = mp.find(complement);
  20. int first = it->first;
  21. int second = arr[i];
  22. cout<<"The pairs are:{"<<first<<","<<second<<"}"<<endl;
  23. return 0;
  24. }
  25. mp[arr[i]] = 1;
  26. }
  27. cout<<"No valid pairs found"<<endl;
  28. return 0;
  29. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
The pairs are:{3,1}