fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. using ll = long long;
  5. #define fo(i,start,end) for(ll i=start;i<end;i++)
  6.  
  7. int main(){
  8. vector<ll>arr = { 1, 5, 2 ,4 ,1};
  9. int k = 3;
  10. ll n = 5;
  11. unordered_map<int,int>mp; // store element -> frequency
  12. fo(j,0,n){
  13. ll query = k + arr[j];
  14. if(mp.find(query) != mp.end()){
  15. //element is present
  16. cout<<"Pairs are {"<<mp[query]<<","<<j<<"}"<<endl;
  17. return 0;
  18. }
  19. mp[arr[j]] = j;
  20. }
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Pairs are {1,2}