


For the given arr[ ] = { 1, 2, 3, 1}
After 0 rotation arr[ ] = { 1, 2, 3, 1} the sum is = (0 *1 + 1 * 2 + 2 * 3 + 3 * 1) = 11.
After 1 rotation arr[ ] = { 1, 1, 2, 3} the sum is = (0 *1 + 1 * 1 + 2 * 2 + 3 * 3) = 14.
After 2 rotation arr[ ] = { 3, 1, 1, 2} the sum is = (0 *3 + 1 * 1 + 2 * 1 + 3 * 2) = 9.
After 3 rotation arr[ ] = { 2, 3, 1, 1} the sum is = (0 *2 + 1 * 3 + 2 * 1 + 3 * 1) = 8.
So the maximum sum is 14 when arr[ ] = { 1, 1, 2, 3}.
The first line of input contains an integer ‘T’, denoting the number of test cases. Then each test case follows.
The first line of each test case contains the Integer ‘N’ denoting the number of elements in the array.
The second and the last line of each test case contains ‘N’ single space-separated integers representing the elements of the array.
For each test case, print an integer representing the maximum value of 0 * arr[0] + 1 * arr[1] + 2 * arr[2] … (N - 1) * arr[N - 1].
The output of each test case will be printed on a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5 * 10 ^ 6
-10 ^ 5 <= arr[i] <= 10 ^ 5
Time Limit: 1 sec.
The idea is to try out all possible rotations and find the value 0 * arr[0] + 1 * arr[1] + 2 * arr[2] … (N - 1) * arr[N - 1]. We will iterate ‘i’ from 0 to N.
The steps are as follows:
The idea is to compute the value of a current rotation using the value of the previous rotation.When an array is rotated by one, the following changes will happen in the previous sum :