-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arithmetic Array.cpp
108 lines (74 loc) · 2.09 KB
/
Arithmetic Array.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Task: An array b of length k is called good if its arithmetic mean is equal to 1. More formally, if
b1+⋯+bkk=1.
Note that the value b1+⋯+bkk
is not rounded up or down. For example, the array [1,1,1,2] has an arithmetic mean of 1.25, which is not equal to 1
.
You are given an integer array a
of length n
. In an operation, you can append a non-negative integer to the end of the array. What's the minimum number of operations required to make the array good?
We have a proof that it is always possible with finitely many operations.
Input
The first line contains a single integer t
(1≤t≤1000) — the number of test cases. Then t
test cases follow.
The first line of each test case contains a single integer n
(1≤n≤50) — the length of the initial array a
.
The second line of each test case contains n
integers a1,…,an (−104≤ai≤104
), the elements of the array.
Output
For each test case, output a single integer — the minimum number of non-negative integers you have to append to the array so that the arithmetic mean of the array will be exactly 1
.
Example
Input
Copy
4
3
1 1 1
2
1 2
4
8 4 6 2
1
-2
Output
Copy
0
1
16
1
Note
In the first test case, we don't need to add any element because the arithmetic mean of the array is already 1
, so the answer is 0
.
In the second test case, the arithmetic mean is not 1
initially so we need to add at least one more number. If we add 0 then the arithmetic mean of the whole array becomes 1, so the answer is 1
.
In the third test case, the minimum number of elements that need to be added is 16
since only non-negative integers can be added.
In the fourth test case, we can add a single integer 4
. The arithmetic mean becomes −2+42 which is equal to 1.
Solution:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ll t;
cin>>t;
while(t--){
ll n,a,b=0;
cin>>n;
for(ll i=0;i<n;i++){
cin>>a;
b+=a;
}
if(b<n){
cout<<1<<endl;
}
else{
cout<<b-n<<endl;
}
}
return 0;
}