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 nth term is ((n * n / 2) – 1).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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


Output:

7

Time Complexity: O(1)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.