fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool duplicate(int n[], int s, int k) {
  5. for (int i = 0; i < s; i++) {
  6. for (int j = i + 1; j < s && j <= k + i; j++) {
  7. if (n[i] == n[j]) {
  8. if (j - i <= k) {
  9. return true;
  10. }
  11. }
  12. }
  13. }
  14. return false;
  15. }
  16.  
  17. int main() {
  18. int n[] = {1, 2, 3, 1, 4, 5};
  19. int k = 3;
  20. int s = sizeof(n) / sizeof(n[0]);
  21.  
  22. if (duplicate(n, s, k)) {
  23. cout << "Yes";
  24. } else {
  25. cout << "No";
  26. }
  27.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Yes