fork download
  1. #include <iostream>
  2. #include<vector>
  3. #include<unordered_map>
  4. using namespace std;
  5.  
  6. int main() {
  7. // your code goes here
  8. vector<int> a={1,3,5,6,1,9,8};
  9. unordered_map<int,int> m;
  10. int k=4;
  11. int dist=-1;
  12. for(int i=0;i<a.size();i++) {
  13. if(m.find(a[i])!=m.end()) {
  14. dist=i-m[a[i]];
  15. if(dist<=k) {
  16. cout<<dist<<endl;
  17. }
  18. }
  19. m[a[i]]=i;
  20. }
  21. return 0;
  22. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
4