fork download
  1. #include<iostream>//Here the k<=k+i for the distance of element should of k
  2. #include<vector>
  3. using namespace std;
  4.  
  5. bool inRange(vector<int>&v,int k){
  6. int t = v.size();
  7. for (int i = 0; i <= t;i++){
  8. for (int j = i + 1; j <= t && (j - i) <= k;j++){
  9. if(v[i]==v[j]){
  10. return true;
  11. }
  12. }
  13. }
  14. return false;
  15. }
  16. int main(){
  17. vector<int> v = {1, 1, 3, 1, 2, 3};
  18. int k = 2;
  19. if(inRange(v,k)){
  20. cout << "There exit pair";
  21. }
  22. else{
  23. cout << "There does not exist pair";
  24. }
  25.  
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
There exit pair