Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int solution(int[] A) {
- // Find the range of racks to consider
- int minRack = Integer.MAX_VALUE;
- int maxRack = Integer.MIN_VALUE;
- for (int rack : A) {
- minRack = Math.min(minRack, rack);
- maxRack = Math.max(maxRack, rack);
- }
- int optimalRack = minRack;
- int maxMinDistance = 0;
- // Iterate over all possible racks in the range
- for (int rack = minRack; rack <= maxRack; rack++) {
- int minDistance = Integer.MAX_VALUE;
- // Calculate the minimum distance from the current rack to all used racks
- for (int usedRack : A) {
- minDistance = Math.min(minDistance, Math.abs(rack - usedRack));
- }
- // Update the optimal rack if the minimum distance is maximized
- if (minDistance > maxMinDistance) {
- maxMinDistance = minDistance;
- optimalRack = rack;
- }
- }
- return optimalRack;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement