# 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

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 <iostream> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find maximum sum of ` `// adjacent elements of permutation of n ` `// To find max sum of permutation ` `function` `maxSum( ` `$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 Code ` ` ` `$n` `= 3; ` ` ` `echo` `intval` `( maxSum(` `$n` `)); ` ` ` `// This code is contributed by akur ` `?> ` |

*chevron_right*

*filter_none*

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.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Minimize the maximum difference of adjacent elements after at most K insertions
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Maximum difference between two elements in an Array
- Maximum difference elements that can added to a set
- Python | Number of elements to be removed such that product of adjacent elements is always even
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Minimize sum of adjacent difference with removal of one element from array
- Min and max length subarray having adjacent element difference atmost K
- Minimum inversions required so that no two adjacent elements are same
- Count of subsets of integers from 1 to N having no adjacent elements
- Find the original Array using XOR values of all adjacent elements
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Number of ways to form an array with distinct adjacent elements