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. Scanner sc = new Scanner(System.in);
  13. int num = sc.nextInt();
  14. int arr[] = new int[num];
  15. for(int i=0;i<num;i++) arr[i]=sc.nextInt();
  16. int k = sc.nextInt();
  17. Map<Integer,Integer> mp= new HashMap<>();
  18. int psum = 0;
  19. int min = num;
  20. mp.put(0,0);
  21. for(int i=0;i<num;i++){
  22. psum += arr[i];
  23. if(mp.containsKey(psum-k)){
  24. int left = mp.get(psum-k);
  25. min=Math.min(min,i-left+1);
  26. }
  27. mp.put(psum,i);
  28. }
  29. System.out.println(min);
  30. }
  31. }
Success #stdin #stdout 0.1s 56228KB
stdin
8
10 5 2 7 1 9 8 7
15
stdout
2