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 k=sc.nextInt();
  16. int[] arr=new int[n+1];
  17. for(int i=1;i<=n;i++){
  18. arr[i]=sc.nextInt();
  19. }
  20. int[] ps=new int[n+1];
  21. for(int i=1;i<=n;i++){
  22. ps[i]=ps[i-1]+arr[i];
  23. }
  24. HashMap<Integer,Integer> map=new HashMap<>();
  25. map.put(0,0);
  26. int maxLen=0;
  27. int count=0;
  28. for(int i=1;i<=n;i++){
  29. int rem=ps[i]-k;
  30. if(map.containsKey(rem)){
  31. if(i-map.get(rem)>maxLen){
  32. maxLen=i-map.get(rem);
  33. count=1;
  34. }else if(i-map.get(rem)==maxLen){
  35. count++;
  36. }
  37. }
  38. if(!map.containsKey(ps[i])){
  39. map.put(ps[i],i);
  40. }
  41. }
  42.  
  43. System.out.println(count+" whose length is "+maxLen);
  44. }
  45. }
Success #stdin #stdout 0.15s 58680KB
stdin
8
15
10 5 2 7 1 9 8 7
stdout
1 whose length is 4