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. int arr[] ={1, 2, 3, 4, 2, 5}; int k = 5;
  14. HashMap<Integer,Integer>map = new HashMap<>();
  15. map.put(0,-1);
  16. int cs =0; int mc[] ={0,arr.length}; int mac[] = {0,0};
  17. int minc =0; int maxc =0;
  18. for(int i=0; i< arr.length; i++){
  19. cs += arr[i];
  20. int temp = cs-k;
  21. if(!map.containsKey(cs)){map.put(cs,i);}
  22. if(map.containsKey(temp)){
  23.  
  24. if(map.get(cs)- map.get(temp)< mc[1]-mc[0]){
  25. mc[1] = map.get(cs);
  26. mc[0] = map.get(temp);
  27. minc = 1;
  28. }
  29. else if(map.get(cs)- map.get(temp) == mc[1]-mc[0]){
  30. mc[1] = map.get(cs);
  31. mc[0] = map.get(temp);
  32. minc ++;
  33. }
  34. if(map.get(cs)- map.get(temp)> mac[1]-mac[0]){
  35. mac[1] = map.get(cs);
  36. mac[0] = map.get(temp);
  37. maxc =1;
  38. }
  39. else if(map.get(cs)- map.get(temp) == mac[1]-mac[0]){
  40. mac[1] = map.get(cs);
  41. mac[0] = map.get(temp);
  42. maxc++;
  43. }
  44. }
  45. }
  46. System.out.println("MAX -->"+maxc);
  47. System.out.println("MIN -->"+minc);
  48. }
  49. }
Success #stdin #stdout 0.09s 53596KB
stdin
Standard input is empty
stdout
MAX -->1
MIN -->1