D. Add One To Some Elements of Subset: Problem Description
D. Add One To Some Elements of Subset: Problem Description
Problem description
Samson received an array of integers 𝑎[0...𝑛] as a gift. His goal is to perform a number of
operations (potentially zero) such that all elements in the array become identical, meaning
𝑎0=𝑎1=⋯=𝑎𝑛. In each operation, he can select certain indices in the array and increase the
elements at those indices by 1. For instance, consider the array 𝑎=[4,2,1,6,2]. Samson could
choose the indices 0, 1, and 3, and increase the corresponding elements by 1. After this
operation, the array would look like 𝑎=[5,3,1,7,2]. What is the minimum number of operations
needed to make all elements in the array equal?
Input
The first line of the input contains a single integer 𝑡 (1 ≤ 𝑡 ≤ 10^4) — the number of test cases in the test.
The following are descriptions of the input test cases.
The first line of the description of each test case contains one integer 𝑛 (1≤𝑛≤50) — the array 𝑎.
The second line of the description of each test case contains 𝑛 integers 𝑎0,𝑎2,…,𝑎𝑛 (1 ≤ 𝑎𝑖 ≤
10^9) — elements of the array 𝑎.
Output
For each test case, println one integer which the minimum number of operations to make all elements
of the array 𝑎 equal.
Example 1
INPUT OUTPUT
3 3
6 4
342412 1
3
1000 1002 998
2
12 11
Explaination: the above testing data includes 3 test-cases
First test case:
• 𝑎=[3,4,2,4,1,2] take 𝑎2,𝑎4 and perform an operation plus one on them, as a result we
get 𝑎=[3,4,3,4,2,2].
• 𝑎=[3,4,3,4,2,2] we take 𝑎0,𝑎4,𝑎5 and perform an operation on them plus one, as a result we
get 𝑎=[4,4,3,4,3,3].
Page 1 of 2
F-Talent Code 2024. 20/10/2024 D. Add One to Some Elements of Subset
•𝑎=[4,4,3,4,3,3] we take 𝑎2,𝑎4,𝑎5 and perform an operation on them plus one, as a result we
get 𝑎=[4,4,4,4,4,4].
ð There are other sequences of 3 operations, after the application of which all elements become equal.
Example 2
INPUT OUTPUT
1 0
31
1111111111111111111111111111111
Explaination: the above testing data includes only 1 test-cases,
This test-case has 31 elements
The third line is value of each element in array a
Page 2 of 2