fork download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static void main(String[] args) throws java.lang.Exception {
  5. int arr[] = {1, 2, 3, 4, 5, 6, 1, 2, 3};
  6. int n = arr.length;
  7. int k = 3;
  8.  
  9. Map<Integer, Integer> mp = new HashMap<>();
  10. for (int i = 0; i < n; i++) {
  11. if (mp.containsKey(arr[i])) {
  12. if (i - mp.get(arr[i]) <= k) {
  13. System.out.println("Duplicate Present <= K");
  14. return;
  15. }
  16. }
  17. mp.put(arr[i], i);
  18. }
  19.  
  20. System.out.println("Duplicate Not Present <= K");
  21. }
  22. }
  23.  
Success #stdin #stdout 0.11s 54752KB
stdin
Standard input is empty
stdout
Duplicate Not Present <= K