fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6.  
  7. int n = sc.nextInt();
  8. if (n <= 1) {
  9. System.out.println(0);
  10. return;
  11. }
  12.  
  13. TreeMap<Integer, Integer> freqMap = new TreeMap<>();
  14. for (int i = 0; i < n; i++) {
  15. int num = sc.nextInt();
  16. freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
  17. }
  18.  
  19. if (freqMap.size() <= 1) {
  20. System.out.println(0);
  21. sc.close();
  22. return;
  23. }
  24.  
  25. int smallestElement = freqMap.firstKey();
  26. int freqOfSmallest = freqMap.get(smallestElement);
  27. int elementsToChange = n - freqOfSmallest;
  28.  
  29. int uniqueElementCount = freqMap.size();
  30. int consolidationSteps = uniqueElementCount - 1;
  31.  
  32. int totalSteps = elementsToChange + consolidationSteps;
  33.  
  34. System.out.println(totalSteps);
  35.  
  36. sc.close();
  37. }
  38. }
  39.  
Success #stdin #stdout 0.14s 54740KB
stdin
3
5 2 1
stdout
4