fork download
  1. import java.util.*;
  2.  
  3. class OldSchoolHero {
  4. public static List<Integer> minOperations(int T, List<int[]> testCases) {
  5. List<Integer> results = new ArrayList<>();
  6.  
  7. for (int t = 0; t < T; t++) {
  8. int[] boxes = testCases.get(t);
  9. int minValue = Arrays.stream(boxes).min().getAsInt();
  10. int totalOps = Integer.MAX_VALUE;
  11.  
  12. // Try all target values from `minValue` to `minValue - 5` (inclusive)
  13. for (int target = minValue; target >= minValue - 5; target--) {
  14. int currentOps = 0;
  15.  
  16. for (int box : boxes) {
  17. int diff = box - target;
  18.  
  19. // Convert the difference to operations: using 5, 2, and 1 denomination
  20. currentOps += diff / 5 + (diff % 5) / 2 + (diff % 5) % 2;
  21. }
  22.  
  23. totalOps = Math.min(totalOps, currentOps);
  24. }
  25.  
  26. results.add(totalOps);
  27. }
  28.  
  29. return results;
  30. }
  31.  
  32. public static void main(String[] args) {
  33. Scanner sc = new Scanner(System.in);
  34.  
  35. int T = sc.nextInt();
  36. List<int[]> testCases = new ArrayList<>();
  37.  
  38. for (int i = 0; i < T; i++) {
  39. int M = sc.nextInt();
  40. int[] boxes = new int[M];
  41. for (int j = 0; j < M; j++) {
  42. boxes[j] = sc.nextInt();
  43. }
  44. testCases.add(boxes);
  45. }
  46.  
  47. List<Integer> results = minOperations(T, testCases);
  48. for (int res : results) {
  49. System.out.println(res);
  50. }
  51.  
  52. sc.close();
  53. }
  54. }
  55.  
Success #stdin #stdout 0.16s 54760KB
stdin
1
3
7 10 12
stdout
3