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. int b[]=new int[n];
  16. int k=sc.nextInt();
  17. for(int i=0;i<n;i++)
  18. b[i]=sc.nextInt();
  19.  
  20. Map<Integer,Integer> m=new HashMap<>();
  21.  
  22. int ans=Integer.MAX_VALUE;
  23. for(int i=0;i<n;i++)
  24. {
  25. int cpl=k-b[i];
  26. if(m.containsKey(cpl))
  27. {
  28. int len=i-m.get(cpl)+1;
  29. ans=Math.min(len,ans);
  30. }
  31. m.put(b[i],i);
  32. }
  33.  
  34. System.out.println(ans);
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. }
  42. }
Success #stdin #stdout 0.12s 54488KB
stdin
9
8
5 6 7 8 10 4 3 2 1
stdout
7