fork download
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. int n;
  9. cin >> n;
  10.  
  11. vector<int> arr(n);
  12. for(int i = 0; i < n; i++) {
  13. cin >> arr[i];
  14. }
  15.  
  16. int k;
  17. cin >> k;
  18.  
  19. vector<pair<int, int>> ans;
  20.  
  21. unordered_map<int, int> mp;
  22.  
  23. for(int i = 0; i < n; i++) {
  24. int currentElement = arr[i];
  25.  
  26. if(mp.find(currentElement) != mp.end()) {
  27. int lastIndex = mp[currentElement];
  28. int distance = i - lastIndex;
  29.  
  30. if(distance <= k) {
  31. ans.push_back({lastIndex, i});
  32. }
  33. }
  34.  
  35. mp[currentElement] = i;
  36. }
  37.  
  38. for(const auto &p : ans) {
  39. cout << p.first << " : " << p.second << endl;
  40. }
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 5320KB
stdin
5
1
1
3
2
2
1
stdout
0 : 1
3 : 4