Given two **integers N and K**, the task is to **maximise the sum** of absolute differences between adjacent elements of an array of length **N** and sum **K**.

**Examples:**

Input:N = 5, K = 10

Output:20

Explanation:

The array arr[] with sum 10 can be {0, 5, 0, 5, 0}, maximizing the sum of absolute difference of adjacent elements ( 5 + 5 + 5 + 5 = 20)

Input:N = 2, K = 10

Output:10

**Approach:**

To maximize the sum of adjacent elements, follow the steps below:

- If
**N**is 2, the maximum sum possible is**K**by placing**K**in 1 index and**0**on the other. - If
**N**is 1, the maximum sum possible will always be 0. - For all other values of
**N**, the answer will be**2 * K**.

**Illustration:**

For**N = 3**, the arrangement**{0, K, 0}**maximizes the sum of absolute difference between adjacent elements to**2 * K**.

For**N = 4**, the arrangement**{0, K/2, 0, K/2}**or**{0, K, 0, 0}**maximizes the required sum of absolute difference between adjacent elements to**2 * K**.

Below is the implementation of the above approach:

## C++

`// C++ program to maximize the ` `// sum of absolute differences ` `// between adjacent elements ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for maximising the sum ` `int` `maxAdjacentDifference(` `int` `N, ` `int` `K) ` `{ ` ` ` `// Difference is 0 when only ` ` ` `// one element is present ` ` ` `// in array ` ` ` `if` `(N == 1) { ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// Difference is K when ` ` ` `// two elements are ` ` ` `// present in array ` ` ` `if` `(N == 2) { ` ` ` `return` `K; ` ` ` `} ` ` ` ` ` `// Otherwise ` ` ` `return` `2 * K; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 6; ` ` ` `int` `K = 11; ` ` ` ` ` `cout << maxAdjacentDifference(N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to maximize the ` `// sum of absolute differences ` `// between adjacent elements ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function for maximising the sum ` `static` `int` `maxAdjacentDifference(` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `// Difference is 0 when only ` ` ` `// one element is present ` ` ` `// in array ` ` ` `if` `(N == ` `1` `) ` ` ` `{ ` ` ` `return` `0` `; ` ` ` `} ` ` ` ` ` `// Difference is K when ` ` ` `// two elements are ` ` ` `// present in array ` ` ` `if` `(N == ` `2` `) ` ` ` `{ ` ` ` `return` `K; ` ` ` `} ` ` ` ` ` `// Otherwise ` ` ` `return` `2` `* K; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `6` `; ` ` ` `int` `K = ` `11` `; ` ` ` ` ` `System.out.print(maxAdjacentDifference(N, K)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to maximize the ` `# sum of absolute differences ` `# between adjacent elements ` ` ` `# Function for maximising the sum ` `def` `maxAdjacentDifference(N, K): ` ` ` ` ` `# Difference is 0 when only ` ` ` `# one element is present ` ` ` `# in array ` ` ` `if` `(N ` `=` `=` `1` `): ` ` ` `return` `0` `; ` ` ` ` ` `# Difference is K when ` ` ` `# two elements are ` ` ` `# present in array ` ` ` `if` `(N ` `=` `=` `2` `): ` ` ` `return` `K; ` ` ` ` ` `# Otherwise ` ` ` `return` `2` `*` `K; ` ` ` `# Driver code ` `N ` `=` `6` `; ` `K ` `=` `11` `; ` `print` `(maxAdjacentDifference(N, K)); ` ` ` `# This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## C#

`// C# program to maximize the ` `// sum of absolute differences ` `// between adjacent elements ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function for maximising the sum ` `static` `int` `maxAdjacentDifference(` `int` `N, ` `int` `K) ` `{ ` ` ` ` ` `// Difference is 0 when only ` ` ` `// one element is present ` ` ` `// in array ` ` ` `if` `(N == 1) ` ` ` `{ ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// Difference is K when ` ` ` `// two elements are ` ` ` `// present in array ` ` ` `if` `(N == 2) ` ` ` `{ ` ` ` `return` `K; ` ` ` `} ` ` ` ` ` `// Otherwise ` ` ` `return` `2 * K; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 6; ` ` ` `int` `K = 11; ` ` ` ` ` `Console.Write(maxAdjacentDifference(N, K)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

22

* Time Complexity: O(1)*.

## Recommended Posts:

- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Sort an Array based on the absolute difference of adjacent elements
- Minimum absolute difference of adjacent elements in a circular array
- Sort array such that absolute difference of adjacent elements is in increasing order
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Maximize removal of adjacent array elements based on their absolute value
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Absolute Difference of even and odd indexed elements in an Array
- k-th smallest absolute difference of two elements in an array
- Maximum absolute difference between distinct elements in an Array
- Absolute Difference of all pairwise consecutive elements in an array
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Count maximum elements of an array whose absolute difference does not exceed K
- Find K elements whose absolute difference with median of array is maximum
- Search an element in an array where difference between adjacent elements is 1
- Minimize the maximum difference between adjacent elements in an array
- Rearrange array such that difference of adjacent elements is in descending order
- Nth positive number whose absolute difference of adjacent digits is at most 1

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.