# Maximum value of |arr[0] – arr[1]| + |arr[1] – arr[2]| + … +|arr[n – 2] – arr[n – 1]| when elements are from 1 to n

Given an array **arr[]** of size **n** whose elements are from the range **[1, n]**. The task is to find maximum value of **|arr[0] – arr[1]| + |arr[1] – arr[2]| + … +|arr[n – 2] – arr[n – 1]|**. You can arrange the numbers in the array in any order.

**Examples:**

Input:arr[] = {1, 2, 3, 4}

Output:7

Arrange the array in this way for max value, arr[] = {3, 1, 4, 2}

|3 – 1| + |1 – 4| + |4 – 2| = 2 + 3 + 2 = 7

Input:arr[] = {1, 2, 3}

Output:3

We arrange the array as {2, 1, 3}

A **Simple Approach** is to generate all possible permutations. Compute the value for every permutation and find the maximum value.

**Efficient Approach:**

Maximum sum with one element is 0.

Maximum sum with two elements is 1

Maximum sum with three elements is 3 (explained above)

Maximum sum with four elements is 7 (explained above)

It can be observed that for different values of **n**, a pattern for the maximum sum of absolute differences is **0, 1, 3, 7, 11, 17, 23, 31, 39, 49, …..** whose **n ^{th}** term is

**((n * n / 2) – 1)**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the maximum ` `// required value ` `int` `maxValue(` `int` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `0; ` ` ` ` ` `return` `((n * n / 2) - 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `cout << maxValue(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG { ` ` ` ` ` `// Function to return the maximum ` ` ` `// required value ` ` ` `static` `int` `maxValue(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `0` `; ` ` ` ` ` `return` `((n * n / ` `2` `) - ` `1` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.print(maxValue(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` ` ` `# Function to return the maximum ` `# required value ` `def` `maxValue(n): ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `0` ` ` ` ` `return` `(( n ` `*` `n ` `/` `/` `2` `) ` `-` `1` `) ` ` ` `# Driver code ` `n ` `=` `4` `print` `(maxValue(n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG { ` ` ` ` ` `// Function to return the maximum ` ` ` `// required value ` ` ` `static` `int` `maxValue(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `0; ` ` ` ` ` `return` `((n * n / 2) - 1); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` `Console.WriteLine(maxValue(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Function to return the maximum ` `// required value ` `function` `maxValue(` `$n` `) ` `{ ` ` ` `if` `(` `$n` `== 1) ` ` ` `return` `0; ` ` ` ` ` `return` `((` `$n` `* ` `$n` `/ 2) - 1); ` `} ` ` ` `// Driver code ` `$n` `= 4; ` `echo` `maxValue(` `$n` `); ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity:** O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Maximum distinct elements after removing k elements
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Maximum value K such that array has at-least K elements that are >= K
- Maximum in array which is at-least twice of other elements
- Maximum sum such that no two elements are adjacent | Set 2
- Choose n elements such that their mean is maximum
- Sum of maximum elements of all subsets
- Maximum sum such that no two elements are adjacent
- Maximum possible elements which are divisible by 2
- Covering maximum array elements with given value
- Maximum sum of two elements whose digit sum is equal
- Maximum length of the sub-array whose first and last elements are same
- Maximum subset sum such that no two elements in set have same digit in them
- Maximum sum subsequence with at-least k distant elements
- Sum and Maximum of elements in array from [L, R] before and after updates
- Maximum difference between two elements in an Array
- Maximum elements which can be crossed using given units of a and b
- Maximum difference between two subsets of m elements
- Find maximum xor of k elements in an array

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.