fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int nums [] = {1,2,3,2};
  13. int k = 2;
  14. System.out.println(isElepresentWithinK(nums, k));
  15.  
  16. }
  17. static boolean isElepresentWithinK(int nums[], int k){
  18. Map<Integer, Integer> map = new HashMap<>();
  19. for(int i =0; i< nums.length; i++){
  20. if(map.containsKey(nums[i])){
  21. if(Math.abs(map.get(nums[i])-i) <= k) {return true;}
  22. else map.put(nums[i], i);
  23. }else{
  24. map.put(nums[i], i);
  25. }
  26.  
  27. }
  28. return false;
  29.  
  30. }
  31. }
Success #stdin #stdout 0.1s 54728KB
stdin
Standard input is empty
stdout
true