Modify array to maximize sum of adjacent differences
Submissions: 1010   Accuracy:

45.29%

  Difficulty: Medium   Marks: 4

Given an array, we need to modify values of this array in such a way that sum of absolute differences between two consecutive elements is maximized. If the value of an array element is X, then we can change it to either 1 or X.

 

Input:

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains an integer N denoting the size of the array A.

The second line of each test case contains N space separated integers denoting elements of the array A[ ].


Output:

Print the maximum sum of adjacent differences after modifying the array for each test case in a new line.


Constraints:

1<= T <=100

1<= N <=100000

1<= A[ ] <=100000


Example:

Input:

1

5

3 2 1 4 5

Output:

8

Explanation:

Modified array: 3 1 1 4 1

ans = | 1 - 3 | + | 1 - 1 | + | 4 - 1 | + | 1 - 4 | = 8

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Sujnesh Mishra
Author: sujnesh


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.