-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumMedian.java
More file actions
63 lines (49 loc) · 1.94 KB
/
MaximumMedian.java
File metadata and controls
63 lines (49 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Arrays;
import java.util.Scanner;
public class MaximumMedian {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (!scanner.hasNextInt()) {
return;
}
int n = scanner.nextInt();
long k = scanner.nextLong();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
// Sort the array so the median is at index n / 2
Arrays.sort(a);
int medianIndex = n / 2;
long low = a[medianIndex];
long high = a[medianIndex] + k; // The absolute maximum possible median
long ans = low;
// Binary search for the maximum possible median
while (low <= high) {
long mid = low + (high - low) / 2;
if (canAchieveMedian(a, mid, k, medianIndex)) {
ans = mid; // This median is possible, record it
low = mid + 1; // Try to find a larger one
} else {
high = mid - 1; // This median costs too much, aim lower
}
}
System.out.println(ans);
scanner.close();
}
// Helper function to check if we can reach targetMedian within k operations
private static boolean canAchieveMedian(int[] a, long targetMedian, long k, int medianIndex) {
long operationsNeeded = 0;
// We only care about the median and elements to its right
for (int i = medianIndex; i < a.length; i++) {
if (a[i] < targetMedian) {
operationsNeeded += (targetMedian - a[i]);
}
// Early exit optimization: if we exceed k, no need to keep checking
if (operationsNeeded > k) {
return false;
}
}
return operationsNeeded <= k;
}
}