Untitled
unknown
plain_text
2 years ago
891 B
6
Indexable
import java.util.Arrays;
public class MedianAdjustment {
public static long getMinimumMoves(int[] price, int k) {
int n = price.length;
long moves = 0;
// Sort the array
Arrays.sort(price);
// Find the index of the current median
int medianIndex = (n + 1) / 2;
// Calculate the difference between the current median and the target median
int diff = k - price[medianIndex - 1];
// Update each element to match the target median
for (int i = 0; i < n; i++) {
moves += Math.abs(price[i] - (i + 1 + diff));
}
return moves;
}
public static void main(String[] args) {
// Sample Input
int[] price = {4, 2, 1, 4, 7};
int k = 3;
// Sample Output
long result = getMinimumMoves(price, k);
System.out.println(result);
}
}
Editor is loading...
Leave a Comment