# Maximum sum of difference of adjacent elements

Given a number n. We have to find maximum sum of all permutations of n. The maximum sum will be sum of absolute difference of adjacent elements in array.

Examples:

```Input : 3
Output : 3
Permutations of size 3 are:
{1, 2, 3} = 1 + 1
{1, 3, 2} = 2 + 1
{2, 1, 3} = 1 + 2
{2, 3, 1} = 1 + 2
{3, 1, 2} = 2 + 1
{3, 2, 1} = 1 + 1

Input : 2
Output : 1
Permutations of 2 are:
{1, 2} = 1
{2, 1} = 1
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Let us take example of n = 5. We can easily see we can place numbers like 1 5 2 4 3.
abs(1-5) = 4
abs(5-2) = 3
abs(2-4) = 2
abs(4-3) = 1
which sum is 4 + 3 + 2 + 1 = 10.
In general sum of this permutation is n(n-1)/2.
But the maximum sum is obtained if we move 3 at beginning of this permutation ie 3 1 5 2 4.
Sum will become 2 + 4 + 3 + 2 = 12.
We can observe that final relation will become n(n-1)/2 – 1 + n/2 for n > 1.

The permutation of n having maximum sum will be of from n/2, n-1, 2, n-2, 3, n-3. So we have to find sum of this permutation which will be n(n-1)/2 – 1 + n/2.

## C++

 `// CPP program to find maximum sum of ` `// adjacent elements of permutation of n ` `#include ` `using` `namespace` `std; ` ` `  `// To find max sum of permutation ` `int` `maxSum(``int` `n) ` `{ ` `    ``// Base case ` `    ``if` `(n == 1) ` `        ``return` `1; ` ` `  `    ``// Otherwise max sum will ` `    ``// be (n*(n-1)/2) - 1 + n/2 ` `    ``else` `        ``return` `(n * (n - 1) / 2) - 1 + n / 2; ` `} ` ` `  `// Driver program to test maxSum() ` `int` `main() ` `{ ` `    ``int` `n = 3; ` `    ``cout << maxSum(n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find maximum sum of ` `// adjacent elements of permutaion of n ` `public` `class` `Main { ` ` `  `    ``// To find max sum of permutation ` `    ``static` `int` `maxSum(``int` `n) ` `    ``{ ` `        ``// Base case ` `        ``if` `(n == ``1``) ` `            ``return` `1``; ` ` `  `        ``// Otherwise max sum will ` `        ``// be (n*(n-1)/2) - 1 + n/2 ` `        ``else` `            ``return` `(n * (n - ``1``) / ``2``) - ``1` `+ n / ``2``; ` `    ``} ` ` `  `    ``// Driver program to test maxSum() ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``3``; ` `        ``System.out.println(maxSum(n)); ` `    ``} ` `} `

## Python 3

 `# Python program to find maximum sum of ` `# adjacent elements of permutation of n ` ` `  `# To find max sum of permutation ` `def` `maxSum(n): ` ` `  `    ``# Base case ` `    ``if` `(n ``=``=` `1``): ` `        ``return` `1` `  `  `    ``# Otherwise max sum will ` `    ``# be (n*(n-1)/2) - 1 + n / 2 ` `    ``else``: ` `        ``return` `int``((n ``*` `(n ``-` `1``) ``/` `2``) ``-` `1` `+` `n ``/` `2``) ` `  `  `# Driver program to test maxSum() ` `n ``=` `3` `print``(maxSum(n)) ` ` `  `# This code is contributed ` `# by Azkia Anam. `

## C#

 `// C# program to find maximum sum of ` `// adjacent elements of permutaion of n ` `using` `System; ` ` `  `public` `class` `main { ` ` `  `    ``// To find max sum of permutation ` `    ``static` `int` `maxSum(``int` `n) ` `    ``{ ` `         `  `        ``// Base case ` `        ``if` `(n == 1) ` `            ``return` `1; ` ` `  `        ``// Otherwise max sum will ` `        ``// be (n*(n-1)/2) - 1 + n/2 ` `        ``else` `            ``return` `(n * (n - 1) / 2)  ` `                           ``- 1 + n / 2; ` `    ``} ` ` `  `    ``// Driver program to test maxSum() ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 3; ` `         `  `        ``Console.WriteLine(maxSum(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```3
```

This article is contributed by nuclode. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.