fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n,k;
  7. cin>>n>>k;
  8. int arr[n];
  9. for(int i = 0; i < n; i++){
  10. cin>>arr[i];
  11. }
  12.  
  13. unordered_map<int, int> ump;
  14.  
  15. for(int i = 0; i < n; i++){
  16.  
  17. if(ump.find(arr[i]) == ump.end()){
  18. ump[arr[i]] = i;
  19. }
  20. else{
  21. int lastIndex = ump[arr[i]];
  22.  
  23. if(abs(i-lastIndex) <= k){
  24. cout<<"Pair found"<<endl;
  25. ump[arr[i]] = i;
  26. }
  27. }
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0s 5308KB
stdin
5

2

3 2 3 3 1
stdout
Pair found
Pair found