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 = {4,5,5,2,4};
  14. int n = arr.length;
  15. System.out.println("No.of steps to make elements equal "+noOfStepsToMakeAllEqual(arr,n));
  16. }
  17. static int noOfStepsToMakeAllEqual(int [] arr, int n){
  18. TreeMap<Integer,Integer> map = new TreeMap<>(Collections.reverseOrder());
  19. for(int i=0;i<n;i++){
  20. map.put(arr[i],map.getOrDefault(arr[i],0)+1);
  21. }
  22. List<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());
  23. int size = list.size();
  24. int step = 0;
  25. for(int i =0;i<size-1;i++){
  26.  
  27. list.get(i+1).setValue(list.get(i).getValue()+list.get(i+1).getValue());
  28.  
  29. step+= list.get(i).getValue();
  30.  
  31. list.get(i).setValue(0);
  32.  
  33. }
  34. return step;
  35. }
  36. }
Success #stdin #stdout 0.15s 53664KB
stdin
Standard input is empty
stdout
No.of steps to make elements equal 6