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. // your code goes here
  13. Scanner sc=new Scanner(System.in);
  14. int n=sc.nextInt();
  15.  
  16. int a[]=new int[n+1];
  17.  
  18. for(int i=1;i<=n;i++)
  19. a[i]=sc.nextInt();
  20.  
  21. int k=sc.nextInt();
  22.  
  23. Map<Integer,Integer> hash=new HashMap<>();
  24. hash.put(-1,0);
  25.  
  26.  
  27.  
  28. boolean found=false;
  29. for(int i=1;i<=n;i++)
  30. {
  31. if(hash.containsKey(a[i]))
  32. {
  33. if(i-hash.get(a[i])<=k)
  34. found=true;
  35. break;
  36. }
  37. hash.put(a[i],i);
  38. }
  39.  
  40. if(found)
  41. System.out.println("true");
  42. else
  43. System.out.println("false");
  44. }
  45. }
Success #stdin #stdout 0.13s 54516KB
stdin
5
1 3 4 1 1 
3
stdout
true