fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static boolean containsNearbyDuplicateBruteForce(int[] nums, int k) {
  5. int n = nums.length;
  6. for (int i = 0; i < n; ++i) {
  7. for (int j = i + 1; j < n && j <= i + k; ++j) {
  8. if (nums[i] == nums[j]) {
  9. return true;
  10. }
  11. }
  12. }
  13. return false;
  14. }
  15.  
  16. public static void main(String[] args) {
  17. int[] nums = {1, 1, 3, 1, 2, 3};
  18. int k = 2;
  19. if (containsNearbyDuplicateBruteForce(nums, k)) {
  20. System.out.println("There are two equal numbers within distance " + k);
  21. } else {
  22. System.out.println("No two equal numbers found within distance " + k);
  23. }
  24. }
  25. }
Success #stdin #stdout 0.14s 55700KB
stdin
Standard input is empty
stdout
There are two equal numbers within distance 2