Zhan, tired after the contest, gave the only task that he did not solve during the…
- User
Zhan, tired after the contest, gave the only task that he did not solve during the contest to his friend, Sungat. However, he could not solve it either, so we ask you to try to solve this problem.
You are given an array a1,a2,…,an of length n . We can perform any number (possibly, zero) of operations on the array.
In one operation, we choose a position i (1≤i≤n−1 ) and perform the following action:
ai:=ai−1 , and ai+1:=ai+1+1 . Find the minimum possible value of max(a1,a2,…,an)−min(a1,a2,…,an) .
Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤105 ). The description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤2⋅105 ).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤1012 ).
The sum of n over all test cases does not exceed 2⋅105 .
Output For each test case, output a single integer: the minimum possible value of max(a1,a2,…,an)−min(a1,a2,…,an) .
help me god c++ give proof also
- Assistant
The problem involves applying a specific operation on the array to minimize the difference between its maximum and minimum elements. The operation allows us to set
aitoai-1(effectively decreasingaiifai-1is smaller) and increaseai+1by1. We'll need to analyze whether performing this operation actually reduces the difference between the maximum and minimum elements.Key Observations:
-
Operations May Increase the Maximum Element:
- When we decrease to , we simultaneously increase by .
-