fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.List;
  7. import java.util.stream.Collectors;
  8. import java.util.stream.Stream;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static Integer max = -1;
  14.  
  15. private static List<Integer> getNumbersList(String s){
  16. return Arrays.asList(s.split(",")).stream()
  17. .map(x -> x.trim())
  18. .map(x -> Integer.valueOf(x))
  19. .collect(Collectors.toList());
  20. }
  21.  
  22. private static Integer getMax(List<Integer> numbers) {
  23. Integer max = -1;
  24. for(int i = 0; i < numbers.size(); i++){
  25. Integer it = numbers.get(i);
  26. if(it > max)
  27. max = it;
  28. }
  29.  
  30. return max;
  31. }
  32.  
  33. private static List<Integer> getHist(List<Integer> numbers, Integer bucSize) {
  34. max = getMax(numbers);
  35. double x = (max + 0.0) / bucSize;
  36. int maxBucCnt = (int)Math.ceil(x);
  37. //System.out.println(maxBucCnt);
  38. List<Integer> histCnt = new ArrayList<>();
  39.  
  40. for(int i = 0; i < maxBucCnt; i++)
  41. histCnt.add(0);
  42.  
  43. for(int i = 0; i < numbers.size(); i++) {
  44. int it = numbers.get(i);
  45. int iit = (int)Math.ceil((it + 0.0) / bucSize);
  46. Integer prevCnt = histCnt.get(iit - 1);
  47. histCnt.set(iit - 1, prevCnt + 1);
  48. };
  49.  
  50. return histCnt;
  51. }
  52.  
  53.  
  54. public static void main (String[] args) throws java.lang.Exception
  55. {
  56. // your code goes here;
  57. try {
  58. Scanner sc = new Scanner(System.in);
  59. String s = sc.nextLine();
  60. String s2 = sc.nextLine();
  61. Integer bucSize = Integer.valueOf(s2);
  62. List<Integer> numbers = getNumbersList(s);
  63. List<Integer> histCnt = getHist(numbers, bucSize);
  64.  
  65. for(int i = 0; i < histCnt.size(); i++) {
  66. Integer st = i * bucSize + 1;
  67. Integer end = (i + 1) * bucSize;
  68. System.out.println(st + " to " + end + ": " + histCnt.get(i));
  69. }
  70. }catch(Exception ex) {
  71. System.out.println(ex);
  72. }
  73.  
  74. }
  75. }
Success #stdin #stdout 0.17s 2184192KB
stdin
1, -31, -30, -71, 21, 40, 23, 45
10
stdout
java.lang.ArrayIndexOutOfBoundsException: -4