Advertisement
asdfg0998

Untitled

Nov 22nd, 2024
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. public int solution(int[] A) {
  2.         // Find the range of racks to consider
  3.         int minRack = Integer.MAX_VALUE;
  4.         int maxRack = Integer.MIN_VALUE;
  5.        
  6.         for (int rack : A) {
  7.             minRack = Math.min(minRack, rack);
  8.             maxRack = Math.max(maxRack, rack);
  9.         }
  10.        
  11.         int optimalRack = minRack;
  12.         int maxMinDistance = 0;
  13.  
  14.         // Iterate over all possible racks in the range
  15.         for (int rack = minRack; rack <= maxRack; rack++) {
  16.             int minDistance = Integer.MAX_VALUE;
  17.  
  18.             // Calculate the minimum distance from the current rack to all used racks
  19.             for (int usedRack : A) {
  20.                 minDistance = Math.min(minDistance, Math.abs(rack - usedRack));
  21.             }
  22.  
  23.             // Update the optimal rack if the minimum distance is maximized
  24.             if (minDistance > maxMinDistance) {
  25.                 maxMinDistance = minDistance;
  26.                 optimalRack = rack;
  27.             }
  28.         }
  29.  
  30.         return optimalRack;
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement