fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. bool containsNearbyDuplicateBruteForce(const std::vector<int>& nums, int k) {
  7. int n = nums.size();
  8. for (int i = 0; i < n; ++i) {
  9. for (int j = i + 1; j < n && j <= i + k; ++j) {
  10. if (nums[i] == nums[j]) {
  11. return true;
  12. }
  13. }
  14. }
  15. return false;
  16. }
  17.  
  18. int main() {
  19. int n, k;
  20. std::cout << "Enter the number of elements in the array: ";
  21. std::cin >> n;
  22.  
  23. std::vector<int> nums(n);
  24. std::cout << "Enter " << n << " integers:\n";
  25. for (int i = 0; i < n; ++i) {
  26. std::cin >> nums[i];
  27. }
  28.  
  29. std::cout << "Enter the value of k: ";
  30. std::cin >> k;
  31.  
  32. if (containsNearbyDuplicateBruteForce(nums, k)) {
  33. std::cout << "There are two equal numbers within distance " << k << std::endl;
  34. } else {
  35. std::cout << "No two equal numbers found within distance " << k << std::endl;
  36. }
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
Enter the number of elements in the array: Enter 2 integers:
Enter the value of k: No two equal numbers found within distance 0