fork download
  1. /* package whatever; // don't place package name! */
  2. /*mimimum operations to make array elem equal brute-force */
  3.  
  4. import java.util.*;
  5.  
  6. class Ideone {
  7. public static void main(String[] args) throws java.lang.Exception {
  8. int[] arr = {1, 2, 3, 4, 5, 6, 6};
  9. int n = arr.length;
  10.  
  11. int minOperation = Integer.MAX_VALUE;
  12.  
  13. for (int i = 0; i < n; i++) {
  14. int target = arr[i];
  15. int count = 0; // Count elements not equal to target
  16.  
  17. for (int j = 0; j < n; j++) {
  18. if (arr[j] != target) {
  19. count++;
  20. }
  21. }
  22.  
  23. minOperation = Math.min(minOperation, count);
  24. }
  25.  
  26. System.out.println(minOperation);
  27. }
  28.  
  29. }
Success #stdin #stdout 0.07s 54680KB
stdin
Standard input is empty
stdout
5