You are given an array ‘arr’ consisting of ‘N’ integers. Your task is to maximize the value of 0 * arr[0] + 1 * arr[1] + 2 * arr[2] … (N - 1) * arr[N - 1] . You can rotate the array as many times as you wish.
For Example: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.
Output Format:
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.
Note:
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.
2
4
1 2 3 1
5
1 3 10 -2 0
14
51
In the first test case, 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}.
In the second test case, for the given arr[ ] = { 1, 3, 10, -2, 0}
After 0 rotation arr[ ] ={ 1, 3, 10, -2, 0} the sum is = (0 *1+1* 3 +2 *10 +3 *-2 + 4 * 0) = 17.
After 1 rotation arr[ ] ={ 0, 1, 3, 10, -2} the sum is = (0 *0+1* 1 +2 *3 +3 *10 + 4 * -2) = 29.
After 2 rotation arr[ ] ={ -2, 0, 1, 3, 10} the sum is = (0 *-2+1* 0 +2 *1 +3 *3 + 4 * 10) = 51.
After 3 rotation arr[ ] ={ 10, -2, 0, 1, 3} the sum is = (0 *10+1* -2 +2 *0 +3 *1 + 4 * 3) = 13.
After 4 rotation arr[ ] ={ 3, 10, -2, 0, 1} the sum is = (0 *3+1* 10 +2 *-2 +3 *0 + 4 * 1) = 10.
So the maximum sum is 51 when arr[ ] = { -2, 0, 1, 3, 10} .
2
5
1 2 3 4 5
5
1 -2 3 -4 5
40
14
Can you find out maximum value by trying out all possible rotations?
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:
O(N ^ 2) where ‘N’ is the number of elements in the given array.
We will rotate the array ‘N’ times. We will calculate the value by iterating through the array that takes O(N) time for each rotation. Hence the overall time complexity is O(N * N ) = O(N ^ 2).
O(1).
We are not using any extra space. Hence the overall space complexity is O(1).