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; } |
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 = 3print(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
<?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 ?> |
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.
Recommended Posts:
- 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 difference elements that can added to a set
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Minimize sum of adjacent difference with removal of one element from array
- 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
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Find set of m-elements with difference of any two elements is divisible by k
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Maximum absolute difference in an array
- Queries for maximum difference between prime numbers in given ranges
- Program to find the maximum difference between the index of any two different numbers
- Count ways of choosing a pair with maximum difference



