Problem Statement
Amy has an array A, of N integers. Initially all the elements of array are 0, i.e., Ai = 0 (1<=i<=N).
She also has an array B of N integers.
In one operation she can chose an index K, (1<=K<=N) and increase each element of array A, by 1 except AK. (1-based indexing).
For example, N = 4, A = [0, 0, 0, 0], she can choose K = 2 and increase all the elements of array except A2. So, the array becomes A = [1, 0, 1,1]
Find minimum number of operations required for her to make each element of array A, greater than corresponding element in array B, i.e., for each i (1<=i<=N) make Ai>=Bi.
You are given T independent test cases.
Constraints
- 1 <= T <= 5
- 3 <= N <= 105
- 1 <= Bi < = 109
All input values are integers.
Input Format
- First-line contains T.
- First line of each test case consists of a single integer N.
- Second line of each test case consists of N space separated integers denoting the array B.
Output Format
Print in a newline for each test case a single integer denoting the minimum number of operations required for her to make each element of array A, greater than corresponding element in array B, i.e., for each i (1<=i<=N) make Ai>=Bi.
Sample Input 1
1 3 2 1 1Sample Output 1
2Explanation of Sample 1
- Initially A = [0, 0, 0]
- In 1st operation, she choses K = 2, so A = [1, 0, 1]
- In 2nd operation she choses K = 3, so A = [ 2, 1, 1]
- Now for each i (1<=i<=3), Ai = Bi
I tried but not get 100% testcases success , help me to solve it
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine().trim());
String[] s = br.readLine().trim().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(s[i]);
}
int mi = 0;
int v = 0;
int ans = 0;
int max = a[0];
for (int i = 1; i < n; i++) {
if (a[i] > max) max = a[i];
}
System.out.println(max);
Arrays.sort(a);
for (int i = 0; i < n - 2; i = i + 2) {
if ((a[i] == max)) a[i] = 0;
if (a[i + 1] == max) a[i + 1] = 0;
mi = mi + Math.abs(a[i] - a[i + 1]);
}
System.out.println(mi);
for (int i = 0; i < n; i++) {
if (a[i] == 0) a[i] = max;
v = v + (max - a[i]);
}
System.out.println(v);
if (v >= max) ans = max;
else if (mi == 0) ans = max + mi + 1;
else ans = max + mi;
System.out.println(ans);
}