fork download
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Solution {
  5. public static void main(String[] args) throws IOException {
  6. String[] s = br.readLine().split("\\s");
  7. int N = Integer.parseInt(s[0]);
  8. long K = Long.parseLong(s[1]);
  9. int[] arr = new int[N];
  10. s = br.readLine().split("\\s");
  11. for(int i=0;i<N;++i) arr[i] = Integer.parseInt(s[i]);
  12. System.out.println(solve(N,K,arr));
  13. }
  14.  
  15. private static int solve(long N,long k,int[] arr){
  16. Arrays.sort(arr);
  17. if(arr[0] == arr[arr.length-1]) return 0;
  18.  
  19. Map<Integer,Integer> map = new HashMap<>();
  20. int start = arr[0],end = arr[arr.length-1];
  21.  
  22. for(int i=0;i<arr.length;++i){
  23. map.put(arr[i],map.getOrDefault(arr[i]) + 1);
  24. }
  25.  
  26. while(k > 0){
  27. int f1 = map.get(start);
  28. int f2 = map.get(end);
  29.  
  30. if(f1 < k && f2 < k){
  31. if(f1 < f2){
  32. start++;
  33. map.put(start,map.getOrDefault(start) + f1);
  34. k -= f1;
  35. }else{
  36. end--;
  37. map.put(end,map.getOrDefault(end) + f2);
  38. k -= f2;
  39. }
  40. }else if(f1 < k){
  41. start++;
  42. map.put(start,map.getOrDefault(start) + f1);
  43. k -= f1;
  44. }else if(f2 < k){
  45. end--;
  46. map.put(end,map.getOrDefault(end) + f2);
  47. k -= f2;
  48. }else{
  49. break;
  50. }
  51. }
  52.  
  53.  
  54. return end - start;
  55. }
  56. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:4: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
       ^
Main.java:24: error: method getOrDefault in interface Map<K,V> cannot be applied to given types;
           map.put(arr[i],map.getOrDefault(arr[i]) + 1);
                             ^
  required: Object,Integer
  found: int
  reason: actual and formal argument lists differ in length
  where K,V are type-variables:
    K extends Object declared in interface Map
    V extends Object declared in interface Map
Main.java:34: error: method getOrDefault in interface Map<K,V> cannot be applied to given types;
                   map.put(start,map.getOrDefault(start) + f1);
                                    ^
  required: Object,Integer
  found: int
  reason: actual and formal argument lists differ in length
  where K,V are type-variables:
    K extends Object declared in interface Map
    V extends Object declared in interface Map
Main.java:38: error: method getOrDefault in interface Map<K,V> cannot be applied to given types;
                   map.put(end,map.getOrDefault(end) + f2);
                                  ^
  required: Object,Integer
  found: int
  reason: actual and formal argument lists differ in length
  where K,V are type-variables:
    K extends Object declared in interface Map
    V extends Object declared in interface Map
Main.java:43: error: method getOrDefault in interface Map<K,V> cannot be applied to given types;
                map.put(start,map.getOrDefault(start) + f1);
                                 ^
  required: Object,Integer
  found: int
  reason: actual and formal argument lists differ in length
  where K,V are type-variables:
    K extends Object declared in interface Map
    V extends Object declared in interface Map
Main.java:47: error: method getOrDefault in interface Map<K,V> cannot be applied to given types;
                map.put(end,map.getOrDefault(end) + f2);
                               ^
  required: Object,Integer
  found: int
  reason: actual and formal argument lists differ in length
  where K,V are type-variables:
    K extends Object declared in interface Map
    V extends Object declared in interface Map
6 errors
stdout
Standard output is empty