

You are given an array of integers ‘NUMS’. The beauty of this array can be defined as:
The sum of absolute difference of each two consecutive elements.
In one operation you can reverse one subarray of ‘NUMS’. Your task is to find maximum beauty by performing the operation exactly once.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the ‘T’ test cases follow.
The first line of each test case contains a single positive integer ‘N’ denoting the size of the ‘NUMS’ array.
The second line of each test case contains ‘N’ space-separated positive integers denoting the array elements.
Output Format:
For each test case, print a single line containing a single integer denoting the maximum beauty of the array ‘NUMS’ by doing the operation described above exactly once.
The output of each test case will be printed in 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’ <= 10^5
1 <= NUMS[i] <= 10^8
Where ‘N’ is the size of the array ‘NUMS’ and ‘NUMS[i]’ is the ith element of the array.
Time Limit: 1 sec
1
5
5 4 1 7 2
17
The current beauty of the array is |5 - 4| + |4 - 1| + |1 - 7| + |7 - 2| = 15, after reversing the subarray [4, 1, 7, 2] beauty becomes = |5 - 2| + |2 - 7| + |7 - 1| + |1 - 4| = 17, which is the maximum beauty we can obtain.
2
4
4 3 7 1
5
4 5 3 7 6
13
10
For test case 1, the initial beauty value is 1 + 4 + 6 = 11, on reversing the subarray [3, 7, 1] the beauty becomes 3 + 6 + 4 = 13.
For test case 2, reverse the subarray [5, 3, 7] to get a beauty value of 10.
Think of a brute force approach. Check the answer for every subarray.
O(N ^ 3), where N is the size of the array ‘NUMS’.
There are a total of N*(N-1) = N ^ 2 combinations of left and right and we also need to find the beauty of the whole array of size N for each combination thus the net time complexity is O(N ^ 3).
O(N), where N is the size of the array ‘NUMS’.
As we are using an extra array of size ‘N’, thus raising the space complexity to O(N).