-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMedian.java
More file actions
42 lines (34 loc) · 1.08 KB
/
MaxMedian.java
File metadata and controls
42 lines (34 loc) · 1.08 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
public class MaxMedian
{
public static void main(String[] args) {
/********** Given Question : An array and a value k which denotes no of operations ********/
int a[] = new int [] {1, 3, 5, 8, 10, 12, 15, 20, 31};
int k = 77;
///// "An operation is increment the value of any array element A[i] by 1 //////////////////
/////Find max value of median that can be achieved by k operations /////////////////////////
/******************************************************************************************/
int n = a.length;
int m = n/2;
int med = a[m];
int op[] = new int[m+1];
op[0] = 0;
for(int i=1; i<op.length; i++)
{
op[i] = op[i-1] + i*(a[m+i] - a[m+i-1]);
// System.out.println(op[i]);
}
int limit = k >= op[op.length-1] ? (op.length-1) : 0;
for(int i=0; i<op.length; i++)
{
if(k < op[i])
{
limit = i-1;
break;
}
}
int minValue = a[m+limit];
k = k-op[limit];
int maxValue = minValue + k/(limit+1);
System.out.println("maxValue:" + maxValue);
}
}